如何停止线程? [英] How to stop threads?

查看:132
本文介绍了如何停止线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我开始用这样的代码线程:

Hi guys I start threads with such code:

    Thread[] thr;
    private void button1_Click(object sender, EventArgs e)
    {
        decimal value = numericUpDown2.Value;
        int i = 0;
        threads_count = (int)(value);
        thr = new Thread[threads_count];
        for (; i < threads_count; i++)
        {
            thr[i] = new Thread(new ThreadStart(go));
            thr[i].IsBackground = true;
            thr[i].Start();
        }
    }



如何停止所有的人,如果我的条件成为真正的

How to stop all them if my condition become true

推荐答案

若干问题的答案说,中止该线程。 从不放弃一个线程,除非它是一个紧急情况,你要关闭的应用程序即可。

A number of the answers say to abort the thread. Never abort a thread unless it is an emergency situation and you are shutting down the application.

的CLR保证其内部数据结构不被一个线程中止损坏。 这是由CLR提出的有关线程中止的唯一(*)的保证。它专门针对的的保证:

The CLR guarantees that its internal data structures are not corrupted by a thread abort. This is the only (*) guarantee made by the CLR with respect to thread aborts. It specifically does not guarantee:


  • 该线程才会真正将中止。线程可以强化自己免受被终止。

  • 这所有的数据结构,是的的在CLR本身将是两袖清风。螺纹在关键业务的中间中止可以离开BCL的数据结构或用户数据结构中任意不一致的状态。这个神秘以后可能会崩溃的过程。

  • 这锁将被释放。中止线程可能会导致锁被永远举行,它可能会导致死锁,等等

  • That the thread actually will abort. Threads can harden themselves against being terminated.
  • That any data structure that is not in the CLR itself will be uncorrupted. Thread aborts in the middle of crucial operations can leave BCL data structures or user data structures in arbitrarily inconsistent states. This can crash your process mysteriously later.
  • That locks will be released. Aborting threads can cause locks to be held forever, it can cause deadlocks, and so on.

在情况下,我没有说清楚:它的疯狂危险中止线程的,你应该只有这样做,当所有的选择都是糟糕的。

In case I am not being clear: it is insanely dangerous to abort a thread and you should only do so when all the alternatives are worse.

所以如果你想开始一个线程,然后关闭它干净?

So what if you want to start up a thread and then shut it down cleanly?

首先,不这样做。不要一开始摆在首位的线程。启动任务< T> 以取消标记,当你想将其关闭,信号的取消标记

First, don't do that. Don't start a thread in the first place. Start a Task<T> with a cancellation token and when you want to shut it down, signal its cancellation token.

如果你必须启动一个线程,然后启动线,使得有一些机制,使主线程和工作线程可以干净,安全的沟通:我希望你能在这个时候完全关闭自己打倒。

If you do have to start a thread, then start the thread such that there is some mechanism whereby the main thread and the working thread can cleanly and safely communicate "I want you to shut yourself down cleanly at this time".

如果你不知道该怎么做,然后的停止编写多线程代码,直到你学会如何做到这一点。

If you don't know how to do that then stop writing multithreaded code until you learn how to do that.

(*)这是一个小谎言;在CLR也使某些保证相对于螺纹的相互作用中止和特殊的代码区域,如约束的执行区域和finally块。

(*) This is a small lie; the CLR also makes certain guarantees with respect to the interactions of thread aborts and special code regions such as constrained execution regions and finally blocks.

这篇关于如何停止线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆