473,542 Members | 2,660 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Break thread sleep

Hello group.

I have thread in which I perform specific task in loop. I can specify,
by check box, in what periods of time that task should be done, ex.
instantly, with 5min break, with 15min break etc. For now I had it
done in way like that.

while(true)
{
period = checkIfPeriodCh anged();
...
performTask
...
Sleep(period);
}

That works fine, until I want to change period time. For example, when
I set period to 15min, and after performTask I change it again to
1min, I have to wait all ~15min, to put my new settings (1min) into
operation. Is there any way, to break Sleep(period), or should I do it
in very different way? I looked at System.Threadin g.Timer, but i'm not
sure if it'll be working, becouse I don't know how long time execute
performTask will take (differences can be rather wide - from couple
seconds to few minutes).

I will be gratefull for any kind of help.
Masanobu Suga

Oct 3 '07 #1
15 5648
On Oct 3, 11:22 am, suga.masan...@g mail.com wrote:

<snip>
That works fine, until I want to change period time. For example, when
I set period to 15min, and after performTask I change it again to
1min, I have to wait all ~15min, to put my new settings (1min) into
operation. Is there any way, to break Sleep(period), or should I do it
in very different way?
Instead of using Thread.Sleep, use Monitor.Wait, "breaking" it with
Monitor.Pulse.

Jon

Oct 3 '07 #2
<su***********@ gmail.comwrote in message
news:11******** **************@ 19g2000hsx.goog legroups.com...
Hello group.

I have thread in which I perform specific task in loop. I can specify,
by check box, in what periods of time that task should be done, ex.
instantly, with 5min break, with 15min break etc. For now I had it
done in way like that.

while(true)
{
period = checkIfPeriodCh anged();
...
performTask
...
Sleep(period);
}

That works fine, until I want to change period time. For example, when
I set period to 15min, and after performTask I change it again to
1min, I have to wait all ~15min, to put my new settings (1min) into
operation. Is there any way, to break Sleep(period), or should I do it
in very different way? I looked at System.Threadin g.Timer, but i'm not
sure if it'll be working, becouse I don't know how long time execute
performTask will take (differences can be rather wide - from couple
seconds to few minutes).

I will be gratefull for any kind of help.
Masanobu Suga

I suppose *performTask* starts another thread to run the task, why not
simply use Thread.Join instead of Sleep?

Willy.

Oct 3 '07 #3
su***********@g mail.com wrote:
[...]
while(true)
{
period = checkIfPeriodCh anged();
...
performTask
...
Sleep(period);
}

That works fine, until I want to change period time. For example, when
I set period to 15min, and after performTask I change it again to
1min, I have to wait all ~15min, to put my new settings (1min) into
operation. Is there any way, to break Sleep(period), or should I do it
in very different way?
A couple of suggestions:

1) If you want to block with a timeout but still be alertable, you
can use a WaitHandle (eg AutoResetEvent) and call WaitOne() with a
timeout (eg "period"). This is similar to the Monitor method Jon
mentions; it's just a different way of doing the same thing (assuming
you use the Monitor.Wait() overload with a timeout).

2) IMHO, if the goal is to run a specific task at specific
intervals, and you want the user to be able to modify the interval, your
current implementation isn't really the best anyway.

I think it would be better to just use the Forms.Timer class to have an
event raised on a specific time interval, and then use a
BackgroundWorke r or similar to run the task. Rather than having a
thread sitting around waiting to see if the time period changes or
expires, instead you would simply have the UI update the timer period in
response to user actions, and in the event raised by the timer object
assign a thread (via BackgroundWorke r or similar) to actually do the task.

Pete
Oct 3 '07 #4
On 3 Pa , 12:28, "Jon Skeet [C# MVP]" <sk...@pobox.co mwrote:
Instead of using Thread.Sleep, use Monitor.Wait, "breaking" it with
Monitor.Pulse.
That's first class solution but I still have some problems. If I put
Monitor.Wait in loop and break it with Monitor.Pulse putted in event
(radio button click) I won't have continuity - after one iteration
loop will wait for pulse which is only in click event, so only if I'll
click radio button loop will coutinue (only one iteration again). I
thought about adding aditional pulse to other event, like 'add to
list', but I'm not sure yet, if it'll be working good. If that's not
good idea, and I'm still not using Monitor.Wait/Pulse in a good way,
please correct me.

Oct 5 '07 #5
On 3 Pa , 12:41, "Willy Denoyette [MVP]" <willy.denoye.. .@telenet.be>
wrote:
I suppose *performTask* starts another thread to run the task
It's not. performTask is using specific method from 'add reference'
library. I'm not sure if I really need to make separate thread for it.
(And tell you the truth I don't even know how. That perforTask takes
some arguments, and while running it's sending/returning some
information to form).
Oct 5 '07 #6
On 3 Pa , 18:48, Peter Duniho <NpOeStPe...@Nn OwSlPiAnMk.comw rote:
IMHO, if the goal is to run a specific task at specific
intervals, and you want the user to be able to modify the interval, your
current implementation isn't really the best anyway.
Your suggestions are really nice BUT... (always that but ;-) ).
The trick is that I don't know how long time would single loop takes.
So I don't know when use Timer to raise task, after I did it once.
Perhaps in longer periods (like 15min) it won't be a problem, as task
won't take so long to perform, but if I set that it should be
performed without brakes (task, 0 sec break, task, 0 sec break etc.) I
think Timer won't work, as it don't know how long time task will take
(how long it should wait). Generally my break before performing tasks
is performTaskTime + userDefinedBrea kTime.

Oct 5 '07 #7
On Oct 5, 2:36 pm, suga.masan...@g mail.com wrote:
On 3 Pa , 12:28, "Jon Skeet [C# MVP]" <sk...@pobox.co mwrote:
Instead of using Thread.Sleep, use Monitor.Wait, "breaking" it with
Monitor.Pulse.

That's first class solution but I still have some problems. If I put
Monitor.Wait in loop and break it with Monitor.Pulse putted in event
(radio button click) I won't have continuity - after one iteration
loop will wait for pulse which is only in click event, so only if I'll
click radio button loop will coutinue (only one iteration again). I
thought about adding aditional pulse to other event, like 'add to
list', but I'm not sure yet, if it'll be working good. If that's not
good idea, and I'm still not using Monitor.Wait/Pulse in a good way,
please correct me.
It's not clear to me what the problem is, I'm afraid. Could you post a
short but complete program that demonstrates the issue? See
http://pobox.com/~skeet/csharp/complete.html

Jon

Oct 5 '07 #8
<su***********@ gmail.comwrote in message
news:11******** *************@r 29g2000hsg.goog legroups.com...
On 3 Pa , 12:41, "Willy Denoyette [MVP]" <willy.denoye.. .@telenet.be>
wrote:
>I suppose *performTask* starts another thread to run the task

It's not. performTask is using specific method from 'add reference'
library. I'm not sure if I really need to make separate thread for it.
(And tell you the truth I don't even know how. That perforTask takes
some arguments, and while running it's sending/returning some
information to form).



Well it looks like you are calling a method, that in turn calls you back to
update the form.
If the above is correct, then I need some more info:
1. What kind of library is this, this is, what are you adding with "add
reference"?
2. On what thread are you calling this "performTas k", is it on the UI thread
(I hope it's not) or on another thread (I guess it's not) ?

Willy.

Oct 5 '07 #9
su***********@g mail.com wrote:
On 3 Pa , 18:48, Peter Duniho <NpOeStPe...@Nn OwSlPiAnMk.comw rote:
>IMHO, if the goal is to run a specific task at specific
intervals, and you want the user to be able to modify the interval, your
current implementation isn't really the best anyway.

Your suggestions are really nice BUT... (always that but ;-) ).
The trick is that I don't know how long time would single loop takes.
Not really a problem at all. You just need to decide what you want to
do about it.
So I don't know when use Timer to raise task, after I did it once.
If you have a requirement to not start a new task while the current one
is executing, then you need to decide how exactly you want to handle
that case. There are at least two behavior choices:

1) You want the task to be run on specific intervals, and if it's
already running when an interval has expired, just wait for the next
interval to try again.

2) You want the task to be run no more frequently than a specific
interval, but if an interval expires while the task is still being
performed, you want the next iteration of that task to happen
immediately when the current one has completed.

Both options are easily implemented. In the first scenario, you would
simply maintain a volatile flag indicating whether the task is currently
running or not. You'd set the flag when starting the task, and clear it
when the task completes. Then you just look at the flag before starting
the task, and don't bother starting it if it's already set.

In the second scenario, the easiest solution IMHO would be to use a
WaitHandle. You'd have a regular thread (not BackgroundWorke r) that
just sits in a loop waiting on the WaitHandle, and doing the processing
when the handle is set (I'd use an AutoResetEvent for convenience).
Then each time the timer interval expires, you simply set the
WaitHandle. If the task is already ongoing, this will ensure that the
next time it waits on the WaitHandle, it simply continues rather than
actually blocking. If the task isn't already ongoing, then the thread
will be waiting on the WaitHandle and setting it will release the thread
so that it can do the processing you want.

I think one of those two behaviors are most likely to fit your needs.
But if not, you simply need to think about what behavior _would_ fit
your needs and then implement it.
Perhaps in longer periods (like 15min) it won't be a problem, as task
won't take so long to perform, but if I set that it should be
performed without brakes (task, 0 sec break, task, 0 sec break etc.) I
think Timer won't work, as it don't know how long time task will take
(how long it should wait). Generally my break before performing tasks
is performTaskTime + userDefinedBrea kTime.
Just to clarify: the way I read that last sentence is that the interval
between initiation of the performance of your task is variable, and
depends on the user-defined time _and_ the actual time it took to
perform the task.

For example, assuming the user has asked for 10 second intervals, but
the task performance time is variable, you might have a sequence that
looks like this:

Time Activity
----- ------------
0 s perform task, 3 seconds
3 s wait 10 seconds
13 s perform task, 2 seconds
15 s wait 10 seconds
25 s perform task, 8 seconds
33 s wait 10 seconds

etc.

If that's the case, I really don't see what the problem is. That's a
trivial behavior to implement. I think all of us assumed that you
wanted some timing independent of the length of time it takes to
actually perform the task, and our answers have been directed to that goal.

If the answers haven't explained the issue to you well enough yet, it
seems to me you need to consider trying to rephrase your original
question so that it is much more clear about what you actually want to
accomplish.

Pete
Oct 5 '07 #10

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

Similar topics

38
2845
by: Anthony Baxter | last post by:
On behalf of the Python development team and the Python community, I'm happy to announce the release of Python 2.3.1 (final). Python 2.3.1 is a pure bug fix release of Python 2.3, released in late July. A number of obscure crash-causing bugs have been fixed, various memory leaks have been squished, but no new features have been added to the...
26
2349
by: news.microsoft.com | last post by:
Hi, Currently I have a thread thats spinning and doing a Thread.Sleep(someTime). I was thinking of changing this to Thread.Sleep(Timeout.Infinite); then when I have actual data in a collection to process in this thread, I was going to do a _thread.Interrupt(); Would this be a better approach to spinning a thread on ?
8
2768
by: Cider123 | last post by:
I ran into a situation where my Window Service had to process 100,000+ files, when I first noticed I needed to tweak various routines. Everything runs fine, but here's what I ran into: In the routine that loops through the file buffer: for (int i=0;i < _Buffer.length; i++) { // Code here
4
5420
by: Matthew Groch | last post by:
Hi all, I've got a server that handles a relatively high number of concurrent transactions (on the magnitude of 1000's per second). Client applications establish socket connections with the server. Data is sent and received over these connections using the asynchronous model. The server is currently in beta testing. Sporadically over the...
6
1262
by: d_well | last post by:
Hi, I use a method of a dll and this method use 3 or 4 seconds to calculate a result. This method run into a thread. I noticed when I am runing this method, the other thread cannot be run as long as this method isn't finished. So the other threads don't run until the method of the dll is finished, so during this time all my application is...
9
3477
by: Chris Dunaway | last post by:
According to the docs, calling Thread.Sleep(0) causes the thread to be "suspended to allow other waiting threads to execute." What happens if I call Thread.Sleep(500)? Do other threads not get a chance to execute during this time? What is the difference between the two? I have code that runs in a loop like this: Dim dResetTime As...
5
1628
by: fniles | last post by:
I am having problem with thread. I have a Session class with public string variable (called Message) that I set from my Main program. In the session class it checks for the value of Message while inside it's "read loop" waiting for data from the client. In the main program, I check to see if the Message member has been cleared before...
0
2670
by: Buckaroo Banzai | last post by:
Hello, newbie here... I'm writing this program but when I click the start button which should initiate either the Hare or the Tortoise, it does not, this is the first time I use threads, so the problem might be the way I'm passing the value. can someone please take a look at this code and maybe give me some guidance on what I'm doing wrong....
9
6961
by: =?Utf-8?B?anAybXNmdA==?= | last post by:
I've got a routine that builds a table using different queries, different SQL Tables, and adding custom fields. It takes a while to run (20 - 45 seconds) so I wrote a thread to handle the table population. Whenever I call the thread, I pass it a structure containing the table and a few other parameters. The table goes in as a reference,...
0
7726
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...
0
5877
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...
1
5257
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...
0
4884
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...
0
3384
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...
0
3385
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1809
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
1
948
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
626
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...

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.