2

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 OnStop()
    {
        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();
    }
Thomas
  • 99
  • 3
  • 7

2 Answers2

2

Check this example from MS How to: Create and Terminate Threads (C# Programming Guide)

Vlad Bezden
  • 83,883
  • 25
  • 248
  • 179
0
objThread.Abort();

use this in OnStop() method...

kmatyaszek
  • 19,016
  • 9
  • 60
  • 65
Ovais Khatri
  • 3,201
  • 16
  • 14
  • 2
    Please see http://stackoverflow.com/questions/2251964/c-thread-termination-and-thread-abort, http://stackoverflow.com/questions/1559255/whats-wrong-with-using-thread-abort/1560567#1560567, http://msdn.microsoft.com/en-us/library/ty8d3wta.aspx on the dangers of using Thread.Abort() and why you should avoid it. – Bryan Crosby Jun 23 '11 at 15:32