Hi Andrew,
using suspend on a thread is generally considered a bad idea because if
you stop a thread in mid execution you can run into deadlock and race
conditions. What if a thread is suspended inside a critical region, nothing
else will be able to access that region etc. A better way to handle thread
syncronization is through WaitHandles. They act like barriers to thread
execution, threads cannot pass through the WaitHandle until another thread
open the handle allowing the thread to pass through. There are two basic
flavours, AutoResetEvent which will close the handle (go from Signalled to
Unsignalled) once a single thread passes through, and the ManualResetEvent
which once set will allow multiple threads to pass until it is manually reset.
Here is an example where a worker thread is allowed to do some processing
but then must wait for the main Thread to signal to it that it can complete
the final part of its processing:
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace ConsoleApplication18
{
class Program
{
static void Main(string[] args)
{
AutoResetEvent waitHandle = new AutoResetEvent(false);
//start other thread
Worker worker = new Worker();
worker.DoSomeWork(waitHandle);
//let main UI thread so some processing
Console.WriteLine("Main thread working");
Thread.Sleep(5000);
Console.WriteLine("Main thread finished work, let other thread
continue");
waitHandle.Set();
Console.ReadLine();
}
}
class Worker
{
private WaitHandle waitHandle;
public void DoSomeWork(WaitHandle waitHandle)
{
this.waitHandle = waitHandle;
Thread t = new Thread(new ThreadStart(ProcessValues));
t.Start();
}
private void ProcessValues()
{
//Can run this anytime
for (int i = 0; i < 5; i++)
{
Console.WriteLine("processing:" + i);
}
//make sure I am allowed to keep going
this.waitHandle.WaitOne();
//Can only run this once main thread is happy
for (int i = 0; i < 5; i++)
{
Console.WriteLine("last processing:" + i);
}
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace ConsoleApplication18
{
class Program
{
static void Main(string[] args)
{
AutoResetEvent waitHandle = new AutoResetEvent(false);
//start other thread
Worker worker = new Worker();
worker.DoSomeWork(waitHandle);
//let main UI thread so some processing
Console.WriteLine("Main thread working");
Thread.Sleep(5000);
Console.WriteLine("Main thread finished work, let other thread
continue");
waitHandle.Set();
Console.ReadLine();
}
}
class Worker
{
private WaitHandle waitHandle;
public void DoSomeWork(WaitHandle waitHandle)
{
this.waitHandle = waitHandle;
Thread t = new Thread(new ThreadStart(ProcessValues));
t.Start();
}
private void ProcessValues()
{
//Can run this anytime
for (int i = 0; i < 5; i++)
{
Console.WriteLine("processing:" + i);
}
//make sure I am allowed to keep going
this.waitHandle.WaitOne();
//Can only run this once main thread is happy
for (int i = 0; i < 5; i++)
{
Console.WriteLine("last processing:" + i);
}
}
}
}
Hope that helps
Mark Dawson
--
http://www.markdawson.org
"Andrew Bullock" wrote:
Hi,
I'm using a second thread within my program to do some long
calculations, without locking up the UI:
worker = new Thread(new ThreadStart(myClass.Run));
worker.Start();
I want to be able to pause the calculating, so I'm using
worker.Suspend()
and
worker.Resume()
I'm not using any shared variables or such like resources which need to
be protected/mutex but the compiler is telling me that these two
commands are obsolete/depreciated, yet they both still function.
Is there a newer (better) way I should be doing this, or am I OK to be
using these?
Thanks
Andrew