By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,187 Members | 1,041 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,187 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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.