Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / July 2004

Tip: Looking for answers? Try searching our database.

Waiting for background thread.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Graham Allwood - 29 Jul 2004 14:22 GMT
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
Stoitcho Goutsev \(100\) [C# MVP] - 29 Jul 2004 14:52 GMT
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/default.asp?url=/library/en-us/cpguide/html/cp
ovrasynchronousprogrammingoverview.asp


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

Signature

HTH
Stoitcho Goutsev (100) [C# MVP]

> Hi,
>
[quoted text clipped - 18 lines]
>
> Graham
Graham Allwood - 30 Jul 2004 10:13 GMT
Sounds like a good idea. I presume I could then disable the menu option
until the async call had completed.

Thanks.

> Graham,
>
[quoted text clipped - 11 lines]
> 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/default.asp?url=/library/en-us/cpguide/html/cp
ovrasynchronousprogrammingoverview.asp


> More specifically look at the section
> 'Executing a Callback Method When an Asynchronous Call Completes'
[quoted text clipped - 23 lines]
> >
> > Graham

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.