views:

1753

answers:

6

Hello there,

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?

+2  A: 

No, there isn't, but why would you want to? Just start up a new thread, with the same ThreadStart, and the same parameter (if any).

John Saunders
+2  A: 

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.

BFree
A: 

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 ;-).

Emiswelt
This answer is more a workaround than a real answer. You don't even explain why he gets an error. Groo got it right.
Trap
You're right, but when i was a programming beginner, I had several similar problems. I was reading many solutions like that one from groo, and yes, you're completely right, my solution is only a workaround. But the mechanisms that groo describes were too complex for me to understand in my programming-newb times. So I thougt, it may be better to provide a simple "answer" (or workaround) for a programmer not so experienced.
Emiswelt
@Emiswelt: This is the stuff that makes bad programmers. They don't invest enough time to understand what they're doing. Who's going to clean up your Thread.Abort mess when you're gone?
_NT
+12  A: 

Once you have aborted your thread, you cannot start it again.

But your actual problem is that you are aborting your thread. You should never use Thread.Abort().

If your thread should be paused and continued several times, you should consider using other mechanisms (like AutoResetEvent, for example).

[EDIT]

The solution is actually not that complicated - check this post for a concrete example with Start, Pause, Resume and Stop methods. The only thing that you need to do to make it work properly, is to rearrange your background method so that it runs in a loop - that way you can check if a different thread has requested a stop.

Note how Brannon's example never aborts the thread. It only fires an event, and then waits until the thread finishes gracefully.

Groo
A: 

If you really need to interrupt the thread function and resume, you should set a condition and then check it periodically during processing.

That would allow you to stop processing for some amount of time and then resume.

I've used events and Wait calls to accomplish a similar task.

Brad Bruce
A: 

The easiest way is to not abort the thread.

Rhythmic Fistman