2

I am not really sure about what is the best way to deal with operations executed in background threads in an application I am writing.

I am writing it in C# and I am following the MVVM design pattern. Some user actions are expensive and I'd like to run them in a background thread, which is a quite straightforward approach.

The worker thread then modifies my model, which notifies the ViewModel that something changed and finally the change gets propagated to the View.

My problem is that the chain of events triggered would cause the modification of the View from the background thread, when it is only allowed in the UI thread.

I know I can use the UI thread dispatcher but I am not really sure where (could the view-model be the right place?) so I'd like to know if you follow any design pattern to address this situation.

asked Aug 3, 2014 at 12:37

2 Answers 2

1

For scenarios like this, you should be using either BackgroundWorker or Task.

Example code for BackgroundWorker usage:

var bw = new BackgroundWorker();
bw.DoWork += LongRunningBackgroundWork;
bw.RunWorkerCompleted += LongRunningBackgroundWorkCompleted;
bw.RunWorkerAsync();
void LongRunningBackgroundWork(object sender, DoWorkEventArgs e)
{
 // Perform long running task/work
 // Stuff e.Result with results
}
// this runs on the thread that kicked off BackgroundWorker
void LongRunningBackgroundWorkCompleted(object sender, RunWorkerCompletedEventArgs e)
{
 // Access results from e.Result
 // Any exceptions from e.Error
 // Update ViewModel and inturn UI with the results
}

Refer to this MSDN How-to for step-by-step example on BackgroundWorker usage.

Example Code for Task Usage:

var uiScheduler = TaskScheduler.FromCurrentSynchronizationContext();
Task.Run(() =>
{
 // Long running Background work
 // return Results
}).ContinueWith(task =>
{
 // access results from "task.Result"
 // any errors from task.Exception and task.Exception.InnerExceptions
 // Update ViewModel inturn UI with the results
}, uiScheduler); // Pass the UI Scheduler, so that this task runs on UI thread!

And refer to this MSDN How-to for step-by-step example on scheduling work on UI thread.

answered Aug 26, 2014 at 14:38
2

In WinForms, I used InvokeIfRequired to solve this. I haven't worked much with WPF, but this guy writes about WPF equivalent of InvokeIfRequired: http://blog.somecreativity.com/2008/01/10/wpf-equivalent-of-invokerequired/

Not much of an answer, but better than nothing, I hope :)

answered Aug 3, 2014 at 16:25
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.