473,657 Members | 2,461 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Inter rupt, 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.Inter rupt was issued, it would immediately terminate whatever it
was doing with a ThreadInterrupt edException. 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 1818
On Fri, 12 Sep 2008 13:36:00 -0700, Ray Mitchell
<Ra************ *****@meanoldte acher.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************ *****@meanoldte acher.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
2408
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
20102
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 threading.Thread as base classes. I have tried to use call the join method of the python thread objects from C++. But although the call succeeds, the threads don't exit.
5
2596
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 be interrupted to handle an incoming event (and the flow of execution subsequently diverted to the respective event handler). My question is at what point or points could this interruption occur. Will it only occur when the worker thread is...
1
1781
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, sinks events from outside sources. I realize the worker thread will be interrupted to handle an incoming event (and the flow of execution subsequently diverted to the respective event handler). My question is at what point or points could this...
6
23729
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); } The problem is that I don't know how to terminate the thread when my class
20
1415
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 single service running in my process. Thanks!
3
2306
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? if i do, how do i go about doing it? Thanks
5
6025
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. Which means handling the Sleep/Wakeup of the logic thread all by myself. This looks reasonable to me in the way that the UI and the Logic run separately and only send messages to each other. The main disadvantage of thos method is that I have to...
13
3002
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 the process does not end. So I feel confident the problem occurrs here. It appears another reference is created that needs to be closed. Can anyone tell me how to do this? :) Thank you Excel.Workbook workbook =
0
8726
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8603
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7320
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6163
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4151
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1944
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1604
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.