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 3 1763
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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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....
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
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...
|
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...
|
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...
| |