Multithreading: how to specify responsibility of newly created thread [closed]
I'm very new to multithreading and I'm getting kind of confused! Ok, so suppose I create another thread besides the main thread in my program. That thread should be responsible of updating the UI while the main thread does calculations and other stuff. My question how does that newly created thread know it's responsible for updating the UI? How do you specify that?
Only one thread is allowed to update the UI, and that is the main thread. You can use BackgroundWorker to do the secondary work. It can "report progress" or "complete work" and on those events pass a message back to the main thread to do the work.
Only the UI thread can update the UI.
If you are in another thread you'll need to pass it to the UI thread.
string newText = "New Text Here"; //Your thread
this.Invoke((MethodInvoker)delegate {
Label1.Text = newText; // UI thread
});
精彩评论