检查线程之前&QUOT完成了它的方法杀死"它的C# [英] check if thread finished its method before "killing" it c#

查看:113
本文介绍了检查线程之前&QUOT完成了它的方法杀死"它的C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的节目2个线程。 1是处理GUI和另一种是做一些字自动化。让我们称之为GUIThread和的WorkerThread。

本的WorkerThread是通过方法使用递归循环。
该的WorkerThread仅仅是活着,而这样做的话自动化和用户必须能够停止字自动化。因此,我已经实现了GUI一个停止按钮,简单地杀死/终止的WorkerThread。但是,如果我杀的WorkerThread,而它在这有时会导致我的Word文档中(这是一个较长的故事)问题的方法的中间,这就是为什么我要检查的WorkerThread已经完成了从方法/返回我杀人之前吧。

这是我的code做什么,当我点击停止按钮:

  //停止工作线程=在进程停止字自动化
workerThread.Abort();//这个阻塞线程,直到已的WorkerThread中止
而(workerThread.IsAlive);

我自己的建议/解决办法对这个问题是有一个全局变量我可以在的WorkerThread进入和离开的方法每次设置,但这似乎并没有我的权利。我的意思是我认为必须有对付它的一个更简单的方法。


解决方案

  

但是,如果我杀的WorkerThread,而这是在方法的中间有时我的Word文档中会导致一个问题


这就是为什么你不应该杀死一个线程。你不能说什么线程在做什么,它是否是安全的杀人?等等等等。

中止不是做你期望它做的事情。看文档,它是微妙的调用此方法的一般终止线程的。注意这个词通常和并非总是如此。

中止不会一直杀死线程。例如,如果线程运行的非托管code,CLR不会中止线程,而是将等待线程返回到托管code。

同一开头中止当线程处于的约束的执行区域,finally块等。


  

在CLR线程延迟中止对于在一个CER执行code。


例如:尝试运行下面的code和看看会发生什么。

 私人无效IWillNeverReturn()
{
    线程线程=新主题(()=>
    {
        尝试
        {
        }
        最后
        {
            而(真)
            {}
        }
    });
    thread.Start();    Thread.sleep代码(1000);
    Thread.Abort的();
}

让线程决定何时应该完成,告诉线程,它应该尽快,因为它可以停止。你告诉它使用的CancellationToken

如果您谷歌的 Thread.Abort的邪恶的,你会发现很多有用的资源,的这里有一个

I have 2 threads in my program. 1 is handling a GUI and the other is doing some word automation. Lets call them GUIThread and WorkerThread.

The WorkerThread is looping through methods using recursion. The WorkerThread is only alive while doing the word automation and the user must be able to stop the word automation. Therefore I have implemented a "Stop" button on the GUI which simply kills/terminates the 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.

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

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

Sameway Abort will not do its job when thread is in Constrained Execution Region, finally blocks etc.

The CLR delays thread aborts for code that is executing in a 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.

If you google for Thread.Abort Evil, you'll find lot of useful resources, Here is one.

这篇关于检查线程之前&QUOT完成了它的方法杀死"它的C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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