Click to See Complete Forum and Search --> : Stopping thread gracefully
mce
December 29th, 2008, 08:56 AM
I have a thread started using this way, and ThreadFunc keep sending data to a serial port.
ThreadStart l_startThread = new ThreadStart(ThreadFunc);
Thread myThread = new Thread(l_startThread);
myThread.Start();
public void ThreadFunc()
{
//writing to com port for as long as it wants
}
My problem is how to stop and restart this thread? I can use myThread.Abort(), but i read that this is not a graceful way to stop a thread.
I tried the following :- (but object disposed exception is thrown) or sometimes my pc application just got hanged when i step through the followings.
myThread.Interrupt();
myThread.Join();
I want to be able to stop and restart the thread above (which send data to COM port constinuously, until the user decided to stop the thread.
dannystommen
December 29th, 2008, 09:12 AM
thread.Interrupt will not work:
"Thread.Interrupt Method: Interrupts a thread that is in the WaitSleepJoin thread state"
I would use the abort method
private ThreadStart l_startThread = new ThreadStart(ThreadFunc);
private Thread myThread;
public void StartThread(){
myThread = new Thread(l_startThread);
myThread.Start();
}
public void StopThread(){
if (myThread != null)
myThread.Abort();
}
public void ThreadFunc() {
//writing to com port for as long as it wants
}
TheCPUWizard
December 29th, 2008, 09:29 AM
thread.Interrupt will not work:
"Thread.Interrupt Method: Interrupts a thread that is in the WaitSleepJoin thread state"
I would use the abort method
Thread abort should be used only in extremely rare cases (typically when there is NO other possible way, and you need to hard tear down the entire application.
The right way to do this is to have a communication method with the thread. This can be as simple as a synchronization object.
darwen
December 29th, 2008, 11:56 AM
...to follow on from CPUWiz's answer, try something like this :
class MyThreadClass
{
ManualResetEvent _stopEvent;
Thread _thread;
public MyThreadClass()
{
}
public void Start()
{
_stopEvent = new ManualResetEvent(false);
_thread = new Thread(new ThreadStart(ThreadFunction));
_thread.Start();
}
public void Stop()
{
_stopEvent.Set();
_thread.Join(); // block until thread completed
}
private void ThreadFunction()
{
int index = 0;
while (!_stopEvent.WaitOne(0))
{
Thread.Sleep(500);
Console.WriteLine("{0}", index);
++index;
}
}
}
class Program
{
static void Main(string[] args)
{
MyThreadClass example = new MyThreadClass();
example.Start();
Thread.Sleep(10000);
example.Stop();
}
}
The manual reset event is used to signal the thread that it should finish (i.e. just return from the thread function).
Alternatively you can use ansynchronous invocation of delegates to do the business :
class Program
{
delegate void ThreadFunctionHandler(ManualResetEvent eventStop);
static void Main(string[] args)
{
ManualResetEvent stopEvent = new ManualResetEvent(false);
ThreadFunctionHandler threadFunction = new ThreadFunctionHandler(ThreadFunction);
IAsyncResult asyncResult = threadFunction.BeginInvoke(stopEvent, null, null);
Thread.Sleep(10000);
stopEvent.Set();
threadFunction.EndInvoke(asyncResult); // this will block until delegate exits
}
static void ThreadFunction(ManualResetEvent stopEvent)
{
int index = 0;
while (!stopEvent.WaitOne(0))
{
Thread.Sleep(500);
Console.WriteLine("{0}", index);
++index;
}
}
}
Or even use an anonymous method :
class Program
{
delegate void ThreadFunctionHandler();
static void Main(string[] args)
{
ManualResetEvent stopEvent = new ManualResetEvent(false);
ThreadFunctionHandler threadFunction =
delegate()
{
int index = 0;
while (!stopEvent.WaitOne(0))
{
Thread.Sleep(500);
Console.WriteLine("{0}", index);
++index;
}
};
IAsyncResult asyncResult = threadFunction.BeginInvoke(null, null);
Thread.Sleep(10000);
stopEvent.Set();
threadFunction.EndInvoke(asyncResult); // this will block until delegate exits
}
}
Get the idea ?
Darwen.
Darwen.
mce
December 30th, 2008, 08:52 AM
Thanks for your code samples!! I love samples.. save me a lot of time figuring it out myself. :wave:
Happy New Year 2009!!!
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.