How to handle long "threads" in WPF?

Good evening!

I am currently developing a wpf client for some leisure service. communication with the rest-service is not a problem and is performed in an additional assembly (communication interface).

basically:
I have some kind of "search" -kat that executes the method. this method communicates with the service, updates some text fields and the progress bar (to give the user some graphical information as far as we are ...). unfortunately, the server hosting the service is lame a bit, causing some serious response time (about 4 seconds). this, on the other hand, makes my wpf application wait, and it ends: in black, and the titling is β€œnot responding” ...

I already tried to put this execution in another thread, but ... it is logical that I will not get any access to the controls of my wpf window ...

atm, i'm really helpless ... can someone give me a manual processing or solution?

+3
source share
3 answers

This video shows how to create an asynchronous progress bar using WPF and Dispatcher.BeginInvoke . This is a nice, gentle introduction to a simple solution for basic cross-flow issues in WPF.

+5
source

- . - , UI. BackgroundWorker, , . , UI, .

:

BackgroundWorker bw = new BackgroundWorker();
bw.DoWork += new DoWorkEventHandler(bw_DoWork);
bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
bw.RunWorkerAsync(arg);
...

static void bw_DoWork(object sender, DoWorkEventArgs e)
{
    BackgroundWorker worker = (BackgroundWorker)sender;
    int arg = (int)e.Argument;
    e.Result = CallWebService(arg, e);
}

static void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    progressBar.Increment();
}

static void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    label.Text = "Done: " + e.Result.ToString();
}
+5

Dispatcher.BeginInvoke:

Dispatcher.BeginInvoke(new Action(() =>
    {
        // Update your controls here.
    }), null);

BackgroundWorker.

+1

Source: https://habr.com/ru/post/1708922/


All Articles