473,326 Members | 2,196 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,326 software developers and data experts.

Terminating Thread.Sleep but not thread flow

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
Sep 12 '08 #1
2 1798
On Fri, 12 Sep 2008 13:36:00 -0700, Ray Mitchell
<Ra*****************@meanoldteacher.comwrote:
[...] 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?
As you've found through experimentation, Thread.Sleep() isn't useful for
the purpose you're applying it to. If you only want a thread to yield the
CPU for some specific amount of time, for the express purpose of that
actual delay, then use Thread.Sleep(). Otherwise, don't.

There are a variety of valid approaches to what you're trying to do,
depending on the overall design. Since you haven't described the bigger
picture, it's impossible to provide specific advice. But, you may find
the AutoResetEvent class useful. You can create an instance of that, then
have your thread call the WaitOne() method. Another thread can call Set()
on the AutoResetEvent instance, and the thread waiting at the WaitOne()
method call will then be woken up and continue execution.

You can pass a timeout value to the WaitOne() method and if your thread is
still waiting after that time passes, the method will return anyway. This
allows you to combine the periodic wake-up behavior with the interrupt
wake-up behavior.

That said, you really should try to avoid an implementation where you need
to use the timeout. Whatever your thread is waiting on, it would be much
better if the activity it's waiting on instead could explicitly interrupt
the waiting thread by itself setting the AutoResetEvent instance, or by
using one of the other many asynchronous notification techniques available
in .NET.

Pete
Sep 12 '08 #2


"Peter Duniho" wrote:
On Fri, 12 Sep 2008 13:36:00 -0700, Ray Mitchell
<Ra*****************@meanoldteacher.comwrote:
[...] 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?

As you've found through experimentation, Thread.Sleep() isn't useful for
the purpose you're applying it to. If you only want a thread to yield the
CPU for some specific amount of time, for the express purpose of that
actual delay, then use Thread.Sleep(). Otherwise, don't.

There are a variety of valid approaches to what you're trying to do,
depending on the overall design. Since you haven't described the bigger
picture, it's impossible to provide specific advice. But, you may find
the AutoResetEvent class useful. You can create an instance of that, then
have your thread call the WaitOne() method. Another thread can call Set()
on the AutoResetEvent instance, and the thread waiting at the WaitOne()
method call will then be woken up and continue execution.

You can pass a timeout value to the WaitOne() method and if your thread is
still waiting after that time passes, the method will return anyway. This
allows you to combine the periodic wake-up behavior with the interrupt
wake-up behavior.

That said, you really should try to avoid an implementation where you need
to use the timeout. Whatever your thread is waiting on, it would be much
better if the activity it's waiting on instead could explicitly interrupt
the waiting thread by itself setting the AutoResetEvent instance, or by
using one of the other many asynchronous notification techniques available
in .NET.

Pete
As always Pete, thanks for taking the time for the detailed and informative
explanation. I've implemented AutoResetEvent and it works just like you said
it would. I've apparently got some other issues going on too so you may hear
from me again about this.

Thanks,
Ray
Sep 13 '08 #3

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

Similar topics

9
by: Harald Armin Massa | last post by:
I need to do some synchronisations like in a cron.job import time from threading import Thread class updater(Thread): def run(self): while True: do_updates() time.sleep(600)
3
by: DE | last post by:
Hello, I have an app with embedded Python. Python scripts create their own threads and I need to terminate these threads at the point where the user wants to leave the application. I use...
5
by: Bill Davidson | last post by:
Hello All: I've got a question about synchronization requiremements in a C# worker thread procedure that, among other things, sinks events from outside sources. I realize the worker thread will...
1
by: Bill Davidson | last post by:
(RESEND: I added a little more code to the sample for clarity) Hello All: I've got a question about synchronization requiremements in a C# worker thread procedure that, among other things,...
6
by: Tomaz Koritnik | last post by:
I have a class that runs one of it's method in another thread. I use Thread object to do this and inside ThreadMethod I have an infinite loop: While (true) { // do something Thread.Sleep(100);...
20
by: Lee Schipper | last post by:
If a service needs to shut itself down, say due to a fatal error detected in the program, what is the cleanest way to do that? Can I simply use the Visual Basic End statement? I only have a...
3
by: Astan Chee | last post by:
Hi, Im rather new to threads in python but Im trying to terminate a function call (or the execution of the function) after a certain period of time has passed. Do i need to use threads with this?...
5
by: salberts | last post by:
Hi, I am writing an application that has its UI and Logic layers. My initial idea was to launch the two layers on two different threads and manage calls made by the UI to the Logic manually....
13
by: chuckie_9497 | last post by:
hello all you gurus. I am struggling with releasing com objects. I have isolated the problem to the code below. Objects are released and the process ends until I use "int k = sheet.Count;" Then...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.