I used to use the BackgroundWorker
in some old Windows Forms applications. Now trying out WPF/XAML I don't see it in the toolbox. Essentially, I want to do some large DB queries without tying up the GUI thread. What is the preferred method in WPF?
Asked
Active
Viewed 185 times
0

user17753
- 3,083
- 9
- 35
- 73
-
The Background work is still there in WPF. You would use it in your model to do your long running queries. So it is all instantiated through code this time. There are loads of examples around on using it, and some good tutorials :-) – Pheonyx Mar 11 '15 at 14:00
-
http://stackoverflow.com/questions/3762576/wpf-backgroundworker-vs-dispatcher – Tomasz Jaskuλa Mar 11 '15 at 14:01
-
4I would recommend a asynchronous method for this providing you are targeting .Net 4.5 here is a WPF example: http://www.codearsenal.net/2012/11/csharp-5-async-and-await-example.html#.VQBKpeFOrX8 – David B Mar 11 '15 at 14:02
-
You can use the Task-based Asynchronous Pattern available in C#-5.0 – MrScf Mar 11 '15 at 14:02
-
I don't agree with the duplicate. The preferred method for async DB queries in WPF is the real question. – paparazzo Mar 11 '15 at 14:24
-
You can also look into the Task class. Task.Factory.StartNew(()=>{ //on other thread here... }); – JWP Mar 11 '15 at 14:50
2 Answers
1
BackgroundWorker is .NET - it is still there in WPF.
ADO supports asynch callbacks
AsyncCallback callback = new AsyncCallback(HandleCallback);
// Once the BeginExecuteNonQuery method is called,
// the code continues--and the user can interact with
// the form--while the server executes the query.
command.BeginExecuteNonQuery(callback, command);

paparazzo
- 44,497
- 23
- 105
- 176
0
There is no preferred method.
You can use TPL calls, BackgroundWorker
or even normal asynchronous methods.
The key here is that any updates to UI objects, just like in WinForms, need to be invoked on the correct thread. This is done via Dispatcher.Invoke()
calls.

toadflakz
- 7,764
- 1
- 27
- 40
-
What's the general opinion of updating UI objects on different threads? I try to avoid it if I can, it just doesn't feel right. – Mike Eason Mar 11 '15 at 14:13
-
It depends on the circumstances. If you have a long running process reporting progress, you have no choice but to update the UI from that particular thread if you want meaningful notifications to the user beyond an indefinite ProgressBar/BusyIndicator. – toadflakz Mar 11 '15 at 14:48