UI Threat

  • Thread starter Thread starter XingZheng
  • Start date Start date
X

XingZheng

Hi

I write application that use two threads. the first is UI thread and the
second is non UI thread. my problem is the UI thread must wait for the
non-UI thread complete. So the User Interface is hang. Could you tell me how
to resolve this problem?

Thanks
XingZheng
 
Couple ways. I like this way:
1) Put your worker thread in a class. Make Start(), Stop() methods on it.
2) Have a public Stats struct or class on the worker as Property.
3) Create your worker and start it.
4) Start a form timer
5) Use the timer event function to monitor your threads Stat struct.
6) As it completes work, post some progress using any control you want.
7) If it completes, post that and turn off form timer.
8) Form will be responsive. So you can also have a Cancel button to stop
the worker if needed.

HTH
 
William Stacey said:
Couple ways. I like this way:
1) Put your worker thread in a class. Make Start(), Stop() methods on it.
2) Have a public Stats struct or class on the worker as Property.
3) Create your worker and start it.
4) Start a form timer
5) Use the timer event function to monitor your threads Stat struct.
6) As it completes work, post some progress using any control you want.
7) If it completes, post that and turn off form timer.
8) Form will be responsive. So you can also have a Cancel button to stop
the worker if needed.

Note that if you do this, you need to synchronize updates to the Stats
struct/class so that a) you get complete updates rather than partial
ones, and b) the UI thread is guaranteed to see the changes made by the
worker thread.
 
Most true. Forgot the most important part. Sync all access to struct/class
vars with a lock. A normal lock() is probably the fastest in this case.
You could probably also use InterlockedIncrement if just counters.
 
Back
Top