Killing a .NET thread

2019-01-02 21:12发布

I have created a thread running a certain method. But sometimes I would like to kill the thread even if it is still working. How can I do this? I tried Thread.Abort() but it shows up a messagebox saying "Thread aborted". What should I do?

7条回答
不流泪的眼
2楼-- · 2019-01-02 21:15

It is not a good idea to kill a thread. It is better to signal that it should stop and let it end gracefully. There are various different ways of doing this.

  • Use Thread.Interrupt to poke it if it is blocked.
  • Poll a flag variable.
  • Use the WaitHandle class to send a signal.

There is no need for me to rehash how each method can be used since I have already done so in this answer.

查看更多
无与为乐者.
3楼-- · 2019-01-02 21:18

Do not call Thread.Abort()!

Thread.Abort is dangerous. Instead you should cooperate with the thread so that it can be peacefully shut down. The thread needs to be designed so that it can be told to kill itself, for instance by having a boolean keepGoing flag that you set to false when you want the thread to stop. The thread would then have something like

while (keepGoing)
{
    /* Do work. */
}

If the thread may block in a Sleep or Wait then you can break it out of those functions by calling Thread.Interrupt(). The thread should then be prepared to handle a ThreadInterruptedException:

try
{
    while (keepGoing)
    {
        /* Do work. */
    }
}
catch (ThreadInterruptedException exception)
{
    /* Clean up. */
}
查看更多
低头抚发
4楼-- · 2019-01-02 21:18

The most correct and thread-safe way is to use a WaitHandle to signal to the thread when it's supposed to stop. I mostly use ManualResetEvent.

In your thread, you can have:

private void RunThread()
{
    while(!this.flag.WaitOne(TimeSpan.FromMilliseconds(100)))
    {
        // ...
    }
}

where this.flag is an instance of ManualResetEvent. This means that you can call this.flag.Set() from outside the thread to stop the loop.

The WaitOne method will only return true when the flag is set. Otherwise, it will time out after the specified timeout (100 ms in the example) and the thread will run through the loop once more.

查看更多
像晚风撩人
5楼-- · 2019-01-02 21:25

You should really only call Abort() as a last resort. You can use a variable to sync this thread instead:

volatile bool shutdown = false;

void RunThread()
{
   while (!shutdown)
   {
      ...
   }
}

void StopThread()
{
   shutdown = true;
}

This allows your thread to cleanly finish what it was doing, leaving your app in a known good state.

查看更多
梦寄多情
6楼-- · 2019-01-02 21:31

I agree to Jon B

volatile bool shutdown = false;

void RunThread()
{

try
{
    while (!shutdown)
    {
        /* Do work. */
    }
}
catch (ThreadAbortException exception)
{
    /* Clean up. */
}
}

void StopThread()
{
   shutdown = true;
}
查看更多
余生请多指教
7楼-- · 2019-01-02 21:33

Aborting a thread is a very bad idea, since you cannot determine what the thread was doing at the time of the abort.

Instead, have a property that the thread can check, and that your external code can set. Let the thread check this boolean property when it's at a safe place to exit.

查看更多
登录 后发表回答