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 / .NET Framework / General / February 2005

Tip: Looking for answers? Try searching our database.

Should be simple - How to wait for something to happen?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Bob Dankert - 28 Feb 2005 05:22 GMT
Ok, so this seems like it should be simple, and it is probably because I am
so tired that I do not see the answer.  How do I make a function wait for
something to be set in a multi-threaded environment?  For example, I have a
class with a boolean value which is true or false.  In this class, there is
a method running which needs to wait for the boolean to be true.  Another
process will change the boolean from false to true.  What is the best way to
wait for the boolean to be true??

If I do:  while (boolean != false) {}    It eats up 100% of resources and
kills the program.
If I do:  while (boolean != false) {Application.DoEvents();}    It does not
kill the program, but it still uses 98% of the cpu

It seems to me like I am missing something really obvious, I appreciate any
help!!

Thanks,

Bob Dankert
Jon Skeet [C# MVP] - 28 Feb 2005 07:12 GMT
> Ok, so this seems like it should be simple, and it is probably because I am
> so tired that I do not see the answer.  How do I make a function wait for
[quoted text clipped - 8 lines]
> If I do:  while (boolean != false) {Application.DoEvents();}    It does not
> kill the program, but it still uses 98% of the cpu

You shouldn't be doing this in the UI thread anyway, which is where you
call Application.DoEvents. The UI thread needs to be available for UI
events.

> It seems to me like I am missing something really obvious, I appreciate any
> help!!

I think Monitor.Wait/Monitor.Pulse is what you're after.
See http://www.pobox.com/
~skeet/csharp/threads/deadlocks.shtml#monitor.methods

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Bob Dankert - 28 Feb 2005 14:24 GMT
Jon,

That was exactly what I was looking for, as my issue is specifically a
consumer/producer relationship and I was trying to figure out how to
accomplish it.  Thanks a lot, the site looks like it is filled with tons of
good reading!

Bob

>> Ok, so this seems like it should be simple, and it is probably because I
>> am
[quoted text clipped - 25 lines]
> See http://www.pobox.com/
> ~skeet/csharp/threads/deadlocks.shtml#monitor.methods
Cor Ligthert - 28 Feb 2005 08:52 GMT
Bob,

I would in this case not even use an boolean, but throw an event in the
worker thread.

I am almost sure that on Jon's pages there will be a sample how to catch
that event.

Cor

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.