C# – How to handle long running “thread” in WPF


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).

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?

Best Solution

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.


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

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)

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