473,245 Members | 1,386 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,245 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 2086
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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
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...

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.