Welcome Guest Search | Active Topics | Members | Log In | Register

Break thread sleep Options · View
suga.masanobu@gmail.com
Posted: Tuesday, October 02, 2007 8:22:53 PM


Rank: Guest
Groups: Guest

Joined: 9/17/2007
Posts: 11,670
Points: -1,200
Date parsed: 02/10/2007 20:22:53
Date: Wed, 03 Oct 2007 03:22:53 -0700

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 = checkIfPeriodChanged();
...
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.Threading.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

Jon Skeet [C# MVP]
Posted: Tuesday, October 02, 2007 8:28:54 PM


Rank: Guest
Groups: Guest

Joined: 9/17/2007
Posts: 11,670
Points: -1,200
Date parsed: 02/10/2007 20:28:54
Date: Wed, 03 Oct 2007 03:28:54 -0700

On Oct 3, 11:22 am, suga.masan...@gmail.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

Peter Duniho
Posted: Wednesday, October 03, 2007 2:48:37 AM


Rank: Guest
Groups: Guest

Joined: 9/17/2007
Posts: 11,670
Points: -1,200
Date parsed: 03/10/2007 02:48:37
Date: Wed, 03 Oct 2007 09:48:37 -0700

suga.masanobu@gmail.com wrote:
> [...]
> while(true)
> {
> period = checkIfPeriodChanged();
> ...
> 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
BackgroundWorker 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 BackgroundWorker or similar) to actually do the task.

Pete
Willy Denoyette [MVP]
Posted: Wednesday, October 03, 2007 2:41:52 PM


Rank: Guest
Groups: Guest

Joined: 9/17/2007
Posts: 11,670
Points: -1,200
Date parsed: 03/10/2007 14:41:52
Date: Wed, 3 Oct 2007 12:41:52 +0200

<suga.masanobu@gmail.com> wrote in message
news:1191406973.374563.264980@19g2000hsx.googlegroups.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 = checkIfPeriodChanged();
> ...
> 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.Threading.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.

suga.masanobu@gmail.com
Posted: Thursday, October 04, 2007 11:36:01 PM


Rank: Guest
Groups: Guest

Joined: 9/17/2007
Posts: 11,670
Points: -1,200
Date parsed: 04/10/2007 23:36:01
Date: Fri, 05 Oct 2007 06:36:01 -0700

On 3 Pa , 12:28, "Jon Skeet [C# MVP]" <sk...@pobox.com> wrote:
> 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.

suga.masanobu@gmail.com
Posted: Thursday, October 04, 2007 11:42:32 PM


Rank: Guest
Groups: Guest

Joined: 9/17/2007
Posts: 11,670
Points: -1,200
Date parsed: 04/10/2007 23:42:32
Date: Fri, 05 Oct 2007 06:42:32 -0700

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).


suga.masanobu@gmail.com
Posted: Thursday, October 04, 2007 11:54:40 PM


Rank: Guest
Groups: Guest

Joined: 9/17/2007
Posts: 11,670
Points: -1,200
Date parsed: 04/10/2007 23:54:40
Date: Fri, 05 Oct 2007 06:54:40 -0700

On 3 Pa , 18:48, Peter Duniho <NpOeStPe...@NnOwSlPiAnMk.com> wrote:
> 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 + userDefinedBreakTime.

Jon Skeet [C# MVP]
Posted: Friday, October 05, 2007 12:40:06 AM


Rank: Guest
Groups: Guest

Joined: 9/17/2007
Posts: 11,670
Points: -1,200
Date parsed: 05/10/2007 00:40:06
Date: Fri, 05 Oct 2007 07:40:06 -0700

On Oct 5, 2:36 pm, suga.masan...@gmail.com wrote:
> On 3 Pa , 12:28, "Jon Skeet [C# MVP]" <sk...@pobox.com> wrote:
>
> > 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

Willy Denoyette [MVP]
Posted: Friday, October 05, 2007 8:02:12 PM


Rank: Guest
Groups: Guest

Joined: 9/17/2007
Posts: 11,670
Points: -1,200
Date parsed: 05/10/2007 20:02:12
Date: Fri, 5 Oct 2007 18:02:12 +0200

<suga.masanobu@gmail.com> wrote in message
news:1191591752.520043.33910@r29g2000hsg.googlegroups.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 "performTask", is it on the UI thread
(I hope it's not) or on another thread (I guess it's not) ?

Willy.

Users browsing this topic
Guest


Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Main Forum RSS : RSS

YAFPro Theme Created by Jaben Cargman (Tiny Gecko)
Powered by Yet Another Forum.net version 1.9.1.1 (NET v2.0) - 9/10/2007
Copyright © 2003-2006 Yet Another Forum.net. All rights reserved.
This page was generated in 0.188 seconds.