467,923 Members | 1,567 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,923 developers. It's quick & easy.

Handling a thread which has "Sleep()" function in it

Suppose a function which has Sleep() method in it. And I want to
recycle it. I made two buttons which call "Resume()" and "Suspend()".

But It doesn't work. The state of thread "t" will be "WaitSleepJoin"
when it runs because the function it calls - "GenerateEvents()" has
"Sleep()" function.

Can I solve this problem with Thread? I don't want add nasty boll flags
on it.

public class Test
{
private ThreadStart ts = null;
private Thread t = null;

private void GenerateEvents()
{
while(true)
{
SendClick( MousePosition);
Thread.Sleep(500);
}
}

public Test()
{
ts = new ThreadStart( GenerateClicks);
t = new Thread( ts);
t.Start();
}

public Resume()
{
if( t.ThreadState == ThreadState.Suspend)
{
t.Resume();
}
}

public Suspend()
{
if( t.ThreadState != ThreadState.Suspend)
{
t.Suspend();
}
}
}

Nov 17 '05 #1
  • viewed: 1850
Share:
3 Replies
Hi Kiplring,
you do not want to use Suspend and Resume to control your threads, these
methods have some problems and are dangerous, you can get into deadlock
situations.

Instead try to use ResetEvents such as ManualResetEvent and AutoReset
events to syncronize your threads. These are objects that wait for a signal
from other threads that allow them to keep processing. So for example in
your code below change it to:

public class Test
{
private ThreadStart ts = null;
private Thread t = null;
private ManualResetEvent _mre = null;

public void GenerateClicks()
{
while(true)
{
//Do your processing -> send
click event
Thread.Sleep(500);

//if the manual reset event
has not been
//set then the thread will wait here, if it
is
//set then the thread will
pass straight through
_mre.WaitOne();
}
}

public Test()
{
//create the object, true indicates that the
object is in
//a signalled state to begin -> meaning any
thread
//calling _mre.WaitOne() will not block on
that call
_mre = new ManualResetEvent(true);

ts = new ThreadStart( GenerateClicks);
t = new Thread( ts);
t.Start();
}

public void Resume()
{
//set the manual reset event - this will
allow threads
//who are calling _mre.WaitOne to keep passing
//that call without blocking
_mre.Set();
}

public void Pause()
{
//this will cause threads calling
_mre.WaitOne()
//to block on that call until .Set is called
_mre.Reset();
}
}

Hope that herlps
Mark R Dawson.

"ki******@hanmail.net" wrote:
Suppose a function which has Sleep() method in it. And I want to
recycle it. I made two buttons which call "Resume()" and "Suspend()".

But It doesn't work. The state of thread "t" will be "WaitSleepJoin"
when it runs because the function it calls - "GenerateEvents()" has
"Sleep()" function.

Can I solve this problem with Thread? I don't want add nasty boll flags
on it.

public class Test
{
private ThreadStart ts = null;
private Thread t = null;

private void GenerateEvents()
{
while(true)
{
SendClick( MousePosition);
Thread.Sleep(500);
}
}

public Test()
{
ts = new ThreadStart( GenerateClicks);
t = new Thread( ts);
t.Start();
}

public Resume()
{
if( t.ThreadState == ThreadState.Suspend)
{
t.Resume();
}
}

public Suspend()
{
if( t.ThreadState != ThreadState.Suspend)
{
t.Suspend();
}
}
}

Nov 17 '05 #2
You really got the point what I want. A little bit revise is added.

public void Resume()
{
_mre.Set();
}
public void Pause()
{
//_mre.WaitOne() // remove this, if Pause() is called more than
twice continuously "WaitOne()" can cause a problem. A blocked message
flow is activated when the Resume is called. And turn the Resume state
to Pause again.

_mre.Reset();
}

Nov 17 '05 #3
Sorry - the _mre.WaitOne() inside the Pause function was inside one of my
comments, when it was posted it moved to a line outside of the comment, it
should not be on it's own line like you said :-)

Mark.

"ki******@hanmail.net" wrote:
You really got the point what I want. A little bit revise is added.

public void Resume()
{
_mre.Set();
}
public void Pause()
{
//_mre.WaitOne() // remove this, if Pause() is called more than
twice continuously "WaitOne()" can cause a problem. A blocked message
flow is activated when the Resume is called. And turn the Resume state
to Pause again.

_mre.Reset();
}

Nov 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by M B HONG 20 | last post: by
1 post views Thread by Klaus Vestergaard Kragelund | last post: by
2 posts views Thread by [Yosi] | last post: by
10 posts views Thread by Alfonso Morra | last post: by
3 posts views Thread by Gabi | last post: by
2 posts views Thread by Joe | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.