R
Ray Mitchell
Hello,
I have a thread ABC that starts another thread XYX. Thread XYZ monitors
various things and if there is no work to do it calls Thread.Sleep to sleep
for a minute or so. Occasionally thread ABC needs to get thread XYZ's quick
attention. My first attempt at this was to have thread ABC merely set a
global flag variable that thread XYZ would check at specific points in its
execution, and if it saw that the flag was set it would take the appropriate
action. This worked perfectly unless thread XYZ happened to be sleeping, in
which case it wouldn't see the flag until it awakened. I then changed the
code so that thread ABC would do a threadXYZ.Interrupt, which thread XYZ
would then catch. This solved the sleeping problem put produced another
problem. If thread XYX happened to not be sleeping at the time the
threadXYZ.Interrupt was issued, it would immediately terminate whatever it
was doing with a ThreadInterruptedException. The problem is that I only want
thread XYZ to stop its normal program flow at specific points, that is, at
the points where it checks the global flag, but I want an immediate
termination of its sleep. What is the correct way to achieve this?
Thanks,
Ray
I have a thread ABC that starts another thread XYX. Thread XYZ monitors
various things and if there is no work to do it calls Thread.Sleep to sleep
for a minute or so. Occasionally thread ABC needs to get thread XYZ's quick
attention. My first attempt at this was to have thread ABC merely set a
global flag variable that thread XYZ would check at specific points in its
execution, and if it saw that the flag was set it would take the appropriate
action. This worked perfectly unless thread XYZ happened to be sleeping, in
which case it wouldn't see the flag until it awakened. I then changed the
code so that thread ABC would do a threadXYZ.Interrupt, which thread XYZ
would then catch. This solved the sleeping problem put produced another
problem. If thread XYX happened to not be sleeping at the time the
threadXYZ.Interrupt was issued, it would immediately terminate whatever it
was doing with a ThreadInterruptedException. The problem is that I only want
thread XYZ to stop its normal program flow at specific points, that is, at
the points where it checks the global flag, but I want an immediate
termination of its sleep. What is the correct way to achieve this?
Thanks,
Ray