How to handle long running “thread” in WPF?

2020-02-11 06:46发布

good evening!

currently i'm developing a wpf-client for some rest-service. the communcation with the rest-service is no problem and is done in an extra assembly (communcation-interface).

basically:
i have a somehow "search"-button which executes a method. this method communicates with the service, updates some textboxes and a progress-bar (to give the user some graphic info, how far we are ...). unfortunaly the server, which hosts the service is a bit lame, causing some severe response-time (about 4 secs). this, on the other hand, causes my wpf-application to wait, which ends up in: going black, and titeling "not responding" ...

i've already tried to put this execution in another thread, but ... it's logical that i won't get any access to the controls of my wpf-window ...

atm i'm really helpless ... can anyone give me some handeling-routine or a solution?

2条回答
闹够了就滚
2楼-- · 2020-02-11 07:13

Your UI thread is busy waiting on a response from the web service, and isn't available to paint the screen. One good option, is push the service request off to another, non-UI thread. Look into BackgroundWorker, which was designed specifically to make this easy. It handles marshalling of cross-thread calls from non-UI to UI threads.

Roughly:

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();
}
查看更多
Explosion°爆炸
3楼-- · 2020-02-11 07:34

To access your controls from a second thread use Dispatcher.BeginInvoke:

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

Or you can look into using BackgroundWorker.

查看更多
登录 后发表回答