How to terminate a thread with the OnStop Method (C#)
I have a a thread I am trying to stop in the OnStop method, could someone demonstrate how you could terminate / stop a thread with it:
/// <summary>
/// OnStop: Put your stop code here
/// - Stop threads, set final data, etc.
/// </summary>
protected override void OnSt开发者_如何学编程op()
{
base.OnStop();
}
Taking into account, that is start like follows:
/// <summary>
/// OnStart: Put startup code here
/// - Start threads, get inital data, etc.
/// </summary>
/// <param name="args"></param>
protected override void OnStart(string[] args)
{
Thread MyThread = new Thread(new ThreadStart(MyThreadStarter));
MyThread.Start();
base.OnStart(args);
}
private void MyThreadStarter()
{
realtime obj = new realtime();
obj.Starter();
}
Check this example from MS How to: Create and Terminate Threads (C# Programming Guide)
objThread.Abort();
use this in OnStop()
method...
精彩评论