Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the simplest way to do background tasks in Windows.Forms?

Background tasks being stuff that involves network I/O, disk I/O, or other long-running tasks that may or may not take place over a network. It will often intermix with code that updates the GUI, which needs to be running on another thread, the GUI thread.

Simple meaning that when opening a Form.cs file, the source code is as easy or easier to read than before. In practice, the flow of the source code must still read sequentially with regards to the order in which the code is executed, regardless of which thread it executes on. All the supporting fabric must be reusable and hidden away somewhere, rather than being included in each Form.

Googling MSDN: found that the solution officially sanctioned by Microsoft is the System.ComponentModel.BackgroundWorker, which falls (very!) short on the second point.

(There's also an officially sanctioned Silverlight/XAML/3.5 solution model in System.Windows.Threading.Dispatcher.)

like image 723
rosenfield Avatar asked Dec 30 '22 14:12

rosenfield


1 Answers

If you really don't like BackgroundWorker, you could create your own base class for background actions, as I did here.

like image 191
Mark Heath Avatar answered Jan 05 '23 14:01

Mark Heath