如何使用OnStop方法终止线程(C#)

时间:2011-06-23 15:18:54

标签: c# .net windows

我有一个线程,我试图在OnStop方法中停止,有人可以演示如何终止/停止一个线程:

    /// <summary>
    /// OnStop: Put your stop code here
    /// - Stop threads, set final data, etc.
    /// </summary>
    protected override void OnStop()
    {
        base.OnStop();
    }

考虑到这一点,开头如下:

    /// <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();
    }

2 个答案:

答案 0 :(得分:2)

答案 1 :(得分:0)

objThread.Abort();

OnStop()方法...

中使用此功能