473,327 Members | 1,919 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

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
3 2090
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: M B HONG 20 | last post by:
Hi all - I'm trying to emulate a sleep function in javascript. I know of this method for example: window.setTimeout("function1();", 500); However, I was wondering if there was a way to...
1
by: Klaus Vestergaard Kragelund | last post by:
Hi there I cannot seem to get this to work. In LabWindows I want to be able to call the Sleep function. But when I #Include "Windows.h" I get errors that it cannot build: "Redeclearation of...
2
by: [Yosi] | last post by:
I have C# application, in main form I made thead... User click on susspend button which cause to suspend the thread, in the button fuction: mThread.Susspend(); What I should do to wait in this...
10
by: Alfonso Morra | last post by:
Hi, I need help witht he sleep function as follows. I need to be write som code to do the ff: 1. creates a new thread 2. (in the new thread), Sleep for x milliseconds 3. (in the new thread),...
3
by: Stephen Miller | last post by:
I have an ASP.Net application that sends a NetworkStream to a .Net Service, which has a TcpListener listening on a port for the ASP.Net client. When it receives a request it creates a new thread...
3
by: Gabi | last post by:
Hi all, is there any system call on vb that is similler to "Sleep(xxx)" on c++ if so can you give me its format thanks. Thanks
2
by: Joe | last post by:
just looking but cannot find anything... anyone have an idea?
12
by: gagonm | last post by:
HI I am working on Engineering application where we need thread.sleep() function many times. But since sleep interval is not accurate we need an alternative which can mimick functionality of...
3
by: benhania | last post by:
hello i have a problem with the thread.sleep function - when i make a commend (in windowns form app) and after it i use the sleep function the thread first pause and only then execute all the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.