Waiting for background thread.

  • Thread starter Thread starter Graham Allwood
  • Start date Start date
G

Graham Allwood

Hi,

I have a windows forms application that starts a background thread when it
loads. The background thread is intended to load some data from a database,
this data is required later on in the app (say on some menu option being
chosen). Now, if this menu option is chosen I check the state of the
background thread and if it is still running I want to wait until it
finishes.

Can someone tell is it is ok use the following to pause the UI whilst the
background thread finishes:

myBackgroundThreadAsyncResult.AsyncWaitHandle.WaitOne();

Or is there a better way of waiting for this thread to finish. Perhaps using
Join() from the UI thread?


Any comments?

Regards

Graham
 
Graham,

Either will work, however you will have the same problem of blocked UI
thread - freezed UI. Porbably it doesn't make a lot of sense to use a worker
thread if you going to block the main thread anyways.

You may consider using some kind of notification (event) that the worker
thread may fire when it is done. It would be easely achieved by using
thread-pool threads. To do that create a delegate for the method you want to
execute in a worker thread. Call delegate's BeginInvoke methods and pass
AsyncCallback delegate to a method that will execute when the worker thread
finishes

For more info and example looka at MSDN
- off-line -
ms-help://MS.VSCC.2003/MS.MSDNQTR.2003APR.1033/cpguide/html/cpovrasynchronou
sprogrammingoverview.htm

- or on-line-
http://msdn.microsoft.com/library/d...html/cpovrasynchronousprogrammingoverview.asp

More specifically look at the section
'Executing a Callback Method When an Asynchronous Call Completes'
 
Sounds like a good idea. I presume I could then disable the menu option
until the async call had completed.

Thanks.

Stoitcho Goutsev (100) said:
Graham,

Either will work, however you will have the same problem of blocked UI
thread - freezed UI. Porbably it doesn't make a lot of sense to use a worker
thread if you going to block the main thread anyways.

You may consider using some kind of notification (event) that the worker
thread may fire when it is done. It would be easely achieved by using
thread-pool threads. To do that create a delegate for the method you want to
execute in a worker thread. Call delegate's BeginInvoke methods and pass
AsyncCallback delegate to a method that will execute when the worker thread
finishes

For more info and example looka at MSDN
- off-line -
ms-help://MS.VSCC.2003/MS.MSDNQTR.2003APR.1033/cpguide/html/cpovrasynchronou
sprogrammingoverview.htm

- or on-line-
http://msdn.microsoft.com/library/d...html/cpovrasynchronousprogrammingoverview.asp

More specifically look at the section
'Executing a Callback Method When an Asynchronous Call Completes'

--

HTH
Stoitcho Goutsev (100) [C# MVP]


Hi,

I have a windows forms application that starts a background thread when it
loads. The background thread is intended to load some data from a database,
this data is required later on in the app (say on some menu option being
chosen). Now, if this menu option is chosen I check the state of the
background thread and if it is still running I want to wait until it
finishes.

Can someone tell is it is ok use the following to pause the UI whilst the
background thread finishes:

myBackgroundThreadAsyncResult.AsyncWaitHandle.WaitOne();

Or is there a better way of waiting for this thread to finish. Perhaps using
Join() from the UI thread?


Any comments?

Regards

Graham
 
Back
Top