Multithreaded applications

I read articles on MSDN, but my mind is dead (this usually happens when I read MSDN (I’m not offended by MSDN, but your articles sometimes confuse me). And I try to do some “background work” in my application, but I don’t know how to do this. This is just one method. But the application freezes and I need to wait up to 1 - 3 minutes for it to become ... unchanged?

Are there any simple examples that put "a round online site where I can watch / play?"

Thanks everyone

+6
multithreading c # winforms
source share
3 answers

There is already this worthy question with many links to articles that are easier to digest than MSDN.

An article by John Skeet is the simplest and perhaps the most comprehensive one you can start with, and the Joe Duffy series goes deeper into the details. Looking at C # tags and multithreading in Stackoverflow also gives you some good answers.

You can avoid using BackgroundWorker the fastest way to get started and just use Invoke:

 void ButtonClick(object sender,EventArgs e) { Thread thread = new Thread(Worker); thread.Start(); } void Worker() { if (InvokeRequired) { Invoke(new Action(Worker)); return; } MyLabel.Text = "Done item x"; } 

Some people like to use BackgroundWorker in Stackoverflow, others don't (I'm in Camp No. 2).

0
source share
John Scott wrote a great introduction to multithreading in .NET that you could read. It also covers threading in WinForms . It can go among the lines:
 public partial class Form1 : Form { private BackgroundWorker _worker; public Form1() { InitializeComponent(); _worker = new BackgroundWorker(); _worker.DoWork += (sender, e) => { // do some work here and calculate a result e.Result = "This is the result of the calculation"; }; _worker.RunWorkerCompleted += (sender, e) => { // the background work completed, we may no // present the result to the GUI if no exception // was thrown in the DoWork method if (e.Error != null) { label1.Text = (string)e.Result; } }; _worker.RunWorkerAsync(); } } 
+4
source share
Darin already told you this theory.

But you should check the static ThreadPool.QueueUserWorkItem method. It is more convenient.

0
source share

All Articles