Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update UI from multiple worker threads (.NET)

I have a pet project that I'm working on that has multiple worker threads. Outputting everything to the console is getting hard to follow, so I want to develop a UI that will have one output area per thread. I want to know the best way for the threads to send updates to the UI. I have two ideas:

1) Have each thread set a "DataUpdated" flag when new data is available, and have the UI periodically check for new data.

2) Create each thread with a callback to a UI Update(...) method to be called when new data becomes available.

I am currently leaning toward (2) for two reasons: I dislike the idea of "checking" each thread, and because this is my first multithreaded application and (2) seems simpler than it probably is. I want to know:

  • Which option is preferable in terms of simplicity and efficiency?
  • Do you have any tips for implementing (2) or something like it (i.e. more event-driven)?
like image 298
iandisme Avatar asked Jan 19 '10 21:01

iandisme


People also ask

How do you handle the UI update from multiple threads?

1) Have each thread set a "DataUpdated" flag when new data is available, and have the UI periodically check for new data. 2) Create each thread with a callback to a UI Update(...) method to be called when new data becomes available.

How do I change the UI of a worker thread?

In this case, to update the UI from a background thread, you can create a handler attached to the UI thread, and then post an action as a Runnable : Handler handler = new Handler(Looper. getMainLooper()); handler. post(new Runnable() { @Override public void run() { // update the ui from here } });

Can we update UI from thread?

Worker threads However, note that you cannot update the UI from any thread other than the UI thread or the "main" thread. To fix this problem, Android offers several ways to access the UI thread from other threads. Here is a list of methods that can help: Activity.

Can one process run multiple threads in C#?

Answer: Yes, in a single process we execute multiple threads in C# multithreading program.


2 Answers

You can easily implement (2) by creating BackgroundWorker components and doing the work in their DoWork handlers:

BackgroundWorker bw = new BackgroundWorker();
bw.WorkerReportsProgress = true;
bw.DoWork += /* your background work here */;
bw.ProgressChanged += /* your UI update method here */;
bw.RunWorkerAsync();

Each BackgroundWorker can report progress to the UI thread by calling ReportProgress: although this is primarily designed for reporting progress on a bounded process, that's not mandatory -- you can pass your own custom data as well if that's what your UI update requires. You would call ReportProgress from your DoWork handler.

The nice thing about BackgroundWorker is that it takes care of a lot of messy cross-threading details for you. It also conforms to the event-driven model of updates which you (rightly) prefer to explicit callbacks.

like image 58
itowlson Avatar answered Sep 30 '22 21:09

itowlson


I vote for #2 as well but with BackgroundWorkers instead of System.Threading.Threads.

like image 26
Austin Salonen Avatar answered Sep 30 '22 21:09

Austin Salonen