473,396 Members | 1,689 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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
Jul 21 '05 #1
3 1764
Bob Dankert <bo*@nospamnvsn-it.com> wrote:
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
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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #2
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
Jul 21 '05 #3
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

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Bob Dankert <bo*@nospamnvsn-it.com> wrote:
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


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

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

Jul 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

303
by: mike420 | last post by:
In the context of LATEX, some Pythonista asked what the big successes of Lisp were. I think there were at least three *big* successes. a. orbitz.com web site uses Lisp for algorithms, etc. b....
22
by: Alexander Schmolck | last post by:
Two smallish things that have been bugging me; I'm not sure whether they're actually broken or not, but anyway here it goes: 1. ``os.system`` (and co): Shouldn't ``os.system``'s signature really...
2
by: Rob Mayo | last post by:
OK, maybe this is my opinion, maybe these are bugs. Given the folowing: I have a NotifyIcon on my Form, a Context menu associated with the NotifyIcon, and a MenuItem on the ContextMenu set as...
22
by: codefixer | last post by:
Hi, I have a situation where I have to handle the following scenario. The main() must wait for child to complete or the main() must kill the child after 3 seconds and exit. /* Assume...
13
by: William Stacey | last post by:
FYI. /// <summary> /// Author: William Stacey /// Fast and simple way to implement a singleton pattern without resorting /// to nested classes or other static vodo. Can also be easily converted...
12
by: Jeff B. | last post by:
If an object implements the IDisposable interface, should I always call the Dispose method or is just setting it to null and letting the GC handle it sufficient? Here is the pattern I've been...
3
by: Bob Dankert | last post by:
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...
18
by: Zytan | last post by:
I want the same function to be run whether you press Enter or double click the listbox. It seems really verbose to write both handlers to both events everytime, even if they both call the same...
5
by: fussfart | last post by:
I'm trying to do something that should be very simple but isn't working! (I also want to do something somewhat more complicated, but that has to wait until I figure out the simple stuff.) First, I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.