Threading

D

davebythesea

Hi folks,

In the following code I create a thread and start it. I then call
Thread.Sleep() within that thread. Will this cause the thread I created to
Sleep or will it cause the main application Thread to sleep? I'm hoping it
will cause the created Thread to sleep...

class A
{

private Thread thread;

public A()
{
thread = new Thread(new ThreadStart(Sync));

thread.IsBackground = true;
thread.Priority = ThreadPriority.Normal;
thread.Start();
}

private void Sync()
{
while (true)
{
Thread.Sleep(10000);

// do stuff
}
}
}
 
P

Peter

davebythesea said:
Hi folks,

In the following code I create a thread and start it. I then call
Thread.Sleep() within that thread. Will this cause the thread I
created to Sleep or will it cause the main application Thread to
sleep? I'm hoping it will cause the created Thread to sleep...

Thread.Sleep causes the current thread to block - that means the thread
calling the Thread.Sleep method (ie your thread).


Incidentally I wonder if someone can answer me if the following code is
an ok idea?

I set up a "stopEvent" like this:
private ManualResetEvent[] stopEvent = new ManualResetEvent[1];

Then in my thread method I have the following loop:

for (; ; )
{
int index = WaitHandle.WaitAny(stopEvent, 10000, false);
if (index != WaitHandle.WaitTimeout)
{
break;
}

// do stuff ...
}

The point being I can then have a "Stop" method to stop the thread,
like this:

stopEvent[0].Set();



Thanks,
Peter

--
 
M

Marc Gravell

That is correct. Sync will be executed by the worker thread, so the Sleep
applies to the worker thread; the main thread will carry on doing whatever
without interruption.

Marc
 
M

Marc Gravell

Not sure why you need the array and WaitAny - and indeed the other problem
is that it will be spending lots of time waiting, and not much time *doing*
anything. It depends what the system is - if you just want an exit
condition, a volatile bool might suffice, or a [Manual|Auto]ResetEvent using
the overload of WaitOne that accepts a timeout and returns a bool to
indicate whether the flag was set.
In other scenarios Monitor.Pulse and Monitor.Wait might be used to help
synchronize - especially in scenarios like producer/consumer queues when you
expect the queue to be empty, but after adding an item you want the consumer
thread to wake up immediately and do something useful (and likewise when
terminating it).

Marc
 
P

Peter

Marc said:
Not sure why you need the array and WaitAny - and indeed the other
problem is that it will be spending lots of time waiting, and not
much time doing anything. It depends what the system is - if you just
want an exit condition, a volatile bool might suffice, or a
[Manual|Auto]ResetEvent using the overload of WaitOne that accepts a
timeout and returns a bool to indicate whether the flag was set. In
other scenarios Monitor.Pulse and Monitor.Wait might be used to help
synchronize - especially in scenarios like producer/consumer queues
when you expect the queue to be empty, but after adding an item you
want the consumer thread to wake up immediately and do something
useful (and likewise when terminating it).

Hi, thanks for your reply.

I do want the thread to wait, do something, and wait again... The wait
is configurable, could be many minutes or an hour or so. Could this be
a problem with such a long wait? Are there better ways to implement a
"do something every 30 minutes" function than using a thread with a 30
minute sleep?

The problem with using a bool flag is that it will wait for the timeout
before checking the flag and stopping (I think), whereas if I have a
"waitany" then the wait will cease as soon as the stopEvent is set.

I will look at WaitOne to see what it does...

/Peter

--
 
D

davebythesea

Thanks for the confirmation Peter.

I had considered using a bool to control the running of the Thread ie.

private bool bRun = true;
private void Sync()
{
while (bRun)
{
Thread.Sleep(10000);

// do stuff
}
}

private void StopThread()
{
bRun = false;
}

But then if you want to start the Thread again, a call to Start will throw
an Exception?
 
J

Jon Skeet [C# MVP]

davebythesea said:
Thanks for the confirmation Peter.

I had considered using a bool to control the running of the Thread ie.

private bool bRun = true;
private void Sync()
{
while (bRun)
{
Thread.Sleep(10000);

// do stuff
}
}

private void StopThread()
{
bRun = false;
}

That will only work if bRun is volatile. See
http://pobox.com/~skeet/csharp/threads/volatility.shtml
But then if you want to start the Thread again, a call to Start will throw
an Exception?

You can't restart a thread which has terminated - you'd need to create
a new thread instead.
 

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