简体   繁体   English

在“杀死”c#之前检查线程是否完成了它的方法

[英]check if thread finished its method before “killing” it c#

I have 2 threads in my program. 我的程序中有2个线程。 1 is handling a GUI and the other is doing some word automation. 1是处理GUI而另一个是进行一些文字自动化。 Lets call them GUIThread and WorkerThread. 让我们称他们为GUIThread和WorkerThread。

The WorkerThread is looping through methods using recursion. WorkerThread使用递归循环遍历方法。 The WorkerThread is only alive while doing the word automation and the user must be able to stop the word automation. 在执行自动化一词时,WorkerThread只能处于活动状态,用户必须能够停止自动化一词。 Therefore I have implemented a "Stop" button on the GUI which simply kills/terminates the WorkerThread. 因此,我在GUI上实现了一个“停止”按钮,它只是杀死/终止WorkerThread。 However if I kill the WorkerThread while it's in the middle of a method it sometimes causes a problem in my word document (this is a longer story) and that's why I want to check if the WorkerThread has finished/returned from a method before I kill it. 但是,如果我在方法中间杀死WorkerThread它有时会导致我的word文档出现问题(这是一个较长的故事),这就是为什么我要检查WorkerThread是否在我杀死之前已经完成/返回了一个方法它。

This is what my code does when I hit the "Stop" button: 当我点击“停止”按钮时,这就是我的代码所做的事情:

//Stops the worker thread = stops word automation in process
workerThread.Abort();

//This blocks the thread until the workerThread has aborted
while (workerThread.IsAlive) ;

My own suggestion/workaround for the problem was to have a global variable I could set each time the WorkerThread entered and left a method but this doesn't seem right to me. 我自己对这个问题的建议/解决方法是有一个全局变量,我可以在每次输入WorkerThread时设置并留下一个方法,但这对我来说似乎不对。 I mean I think there must be an easier way to deal with it. 我的意思是我认为必须有一种更简单的方法来处理它。

However if I kill the WorkerThread while it's in the middle of a method it sometimes causes a problem in my word document 但是,如果我在方法中间杀死WorkerThread,它有时会导致我的word文档出现问题

This is why you should never kill a thread. 这就是为什么你永远不应该杀死一个线程。 You can't say what the thread was doing, whether it is safe to kill? 你不能说线程在做什么,是否可以安全杀死? etc etc. 等等

Abort isn't doing what you expect it to do. Abort没有做你期望它做的事情。 Look at the documentation , it is subtle Calling this method usually terminates the thread . 看一下文档 ,它很微妙调用这个方法通常会终止线程 Note the word usually and not always. 注意这个词通常而不是总是。

Yes, Abort will not kill the thread always. 是的, Abort不会永远杀死线程。 For example if the thread was running unmanaged code, CLR will not abort the thread, instead it will wait for the thread to return to managed code. 例如,如果线程正在运行非托管代码,CLR将不会中止该线程,而是等待线程返回托管代码。

Sameway Abort will not do its job when thread is in Constrained Execution Region , finally blocks etc. 当线程处于约束执行区域 ,最后阻塞等时,Sameway Abort将无法完成其工作。

The CLR delays thread aborts for code that is executing in a CER. CLR延迟了在CER中执行的代码的线程中止。

For example: Try to run the following code and see what happens. 例如:尝试运行以下代码,看看会发生什么。

private void IWillNeverReturn()
{
    Thread thread = new Thread(() =>
    {
        try
        {
        }
        finally
        {
            while (true)
            { }
        }
    });
    thread.Start();

    Thread.Sleep(1000);
    thread.Abort();
}

Let the thread decide when it should complete, Tell the thread that it should stop as soon as it can. 让线程决定何时完成,告诉线程它应该尽快停止。 You tell it using CancellationToken . 你用CancellationToken告诉它。

If you google for Thread.Abort Evil , you'll find lot of useful resources, Here is one . 如果你谷歌为Thread.Abort Evil ,你会发现很多有用的资源, 这是一个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM