Make the wpf user interface responsive at the click of a button

In my wpf (C #) application, a lengthy process is executed when the user clicks a button. When the button is pressed until the full code is executed, the window freezes and the user cannot complete any other task in the window. How can I make a button code by pressing a button as a background process so that the window becomes responsive to the user. I tried the following method, but was unsuccessful.

private void btn_convert_Click(object sender, RoutedEventArgs e)
{
    Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background, 
    new Action(() => { 

         WorkerMethod();
    }));
}

Where WorkerMethod is a function with all the code. Any suggestions.

Regards,
Sangeetha

+5
source share
6 answers

, .

/ .

Thread t = new Thread(() => WorkerMethod());
t.Start();

:

WPF BackgroundWorker vs. Dispatcher

"" WPF

+5

.NET 4.5, async/await, :

private async void btn_convert_Click(object sender, RoutedEventArgs e)
{
    await WorkerMethod();
}

WorkerMethod:

private Task WorkerMethod()
{
    return Task.Run(() =>
        {
           // your long running task here
        });
}
+10

BackgroundWorker. , GUI .

private void btn_convert_Click(object sender, RoutedEventArgs e)
{
    BackgroundWorker worker = new BackgroundWorker();
    worker.DoWork += WorkerMethod;
    worker.RunWorkerCompleted += WorkerEnded;
    worker.RunWorkerAsync();
}

private void WorkerMethod()
{
    // New thread, cannot access GUI
}

private void WorkerEnded()
{
    // Back on the GUI Thread
    // Worker is finished
}
+7

UI-Dispatcher, . -,

private void btn_convert_Click(object sender, RoutedEventArgs e)
    {
         Thread t = new Thread(new ThreadStart(WorkerMethod));
         //assign STA or MTA, etc....
         t.Start();
    }

" System.Threading.Thread`.

+1

, : , , IHM , ihm, , , :

, STA, .

, :

http://charly-studio.com/blog/a-loader-for-wpf-long-ui-process/

:

private void MainWindow_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    // Long UI process context
    using (new LongUIProcessContext("Wait while loading..."))
    {
        Thread.Sleep(2000);

        // Sub long ui process context
        using (new LongUIProcessContext("Wait while loading 2..."))
        {
            Thread.Sleep(2000);
        } 

        // Another sub long ui process context
        using (new LongUIProcessContext("Wait while loading 3..."))
        {
            Thread.Sleep(2000);
        }
    }
}

, , : long ui process bench

, , :

+1

Try using the Backgroundworker class. You need to register for the DoWork event and create an event handler in which all your processing code will be placed. You start the worker using the RunWorkerAsync method. There is also a method that can report your working success to ReportProgress. RunWorkerCompleted will show you when your worker finishes processing your code. Try searching the Internet after BackgroundWorker and you will find many examples of how to use it.

0
source

All Articles