Alex Rivera | Logout

Restarting a thread in .NET (using C#)

Asked 2009-06-28T13:04:09.243
15

I'm looking for a way to restart a thread that has been stopped by Abort()..

public partial class MyProgram : Form
{
  private Thread MyThread = new Thread(MyFunction);
  private System.Windows.Forms.Button startStopBtn = new System.Windows.Forms.Button();
  public MyProgram()
  {
    MyThread.Start();
    startStopBtn += new EventHandler(doStop);
    startStopBtn.Text = "Stop";
  }
  private static void MyFunction()
  {
    // do something
  }
  private void doStop(object sender, EventArgs e)
  {
    MyThread.Abort();
    startStopBtn -= new EventHandler(doStop);
    startStopBtn += new EventHandler(doStart);
    startStopBtn.Text = "Start";
  }
  private void doStart(object sender, EventArgs e)
  {
    MyThread.Start(); // << Error returned when clicking the button for 2nd time
    startStopBtn -= new EventHandler(doStart);
    startStopBtn += new EventHandler(doStop);
    startStopBtn.Text = "Stop";
  }
}

Any idea?

Edit
Report

3 Answers

19

Simply add MyThread = new Thread(MyFunction) before calling MyThread.Start() in doStart(). Do not create the thread outside of your methods, the space there is thought for declarations.

Please note that killing a thread with thread.Abort() can be very dangerous, as it might cause unexpected behavior or might not correctly dispose resources owned by the thread. You should try to accomplish clean multi threading, like Groo described in his answer.

answered 2009-06-28T13:24:00.763
8

The simple answer is, you can't. Once a thread has been aborted, you can't restart it. Just create a method or something, that returns a Thread object just how you need it. When you need a new Thread, just get it from that method.

answered 2009-06-28T13:07:22.450
-4

I really don't understand why people provide information if they do not know that is correct.. How can a real programmer suspend or stop processing a thread for sometime and then release it and thereby making the code vulnerable... @Brad-- m sorry.. but your idea was not good.. @Rhythmic - You need to work on your way to approach things..

BFree was somewhat right if you people got him the same way he wanted to say.. You just need to re-declare that..

below is the example:

Public Shared Sub ResetAbort()

    Dim ThreadPleaseWait As New Thread(New ThreadStart(AddressOf YourSubName))

    YourThreadName.Start()

    Thread.Sleep(2000)

    YourThreadName.Abort()

End Sub

Now you can use this Sub anywhere you want to start the thread. It will automatically abort the thread.

If you want to start the thread on Button1_click() event and stop it on Button2_Click() event use this:

in Button1_click() event

    Dim ThreadPleaseWait As New Thread(New ThreadStart(AddressOf YourSubName))

    YourThreadName.Start()

in Button2_click() event

    YourThreadName.Start()

doing this way you will abort you thread where ever you want and will initialize it again. You can also use YourThreadName.ThreadState.Running property to check if the thread is running or not(Just to avoid multiple instances of the same thread.....

answered 2011-02-11T23:32:16.303

Your Answer