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

K

kiplring

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();
}
}
}
 
G

Guest

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

kiplring

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();
}
 
G

Guest

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top