简体   繁体   English

当我们在Threading和For条件中调用Abort()方法时? 在中止线程之前是否有任何验证要检查?

[英]When we call Abort() method in Threading,and For conditions? Is there any validations to check before Abort the thread?

I'm getting ThreadAbortException when call Abort() method, and I referred to solve this in Stack Overflow that they said to use Thread.ResetAbort() method before ending the thread, but It's not clear to me. 我在调用Abort()方法时收到ThreadAbortException ,我在堆栈溢出中提到要解决此问题,他们说在结束线程之前使用Thread.ResetAbort()方法,但是我不清楚。

Please suggest me where can I use ResetAbort method. 请建议我在哪里可以使用ResetAbort方法。 I'm getting that exception in marked line when call the Abort() method. 调用Abort()方法时,我在标记行中得到了该异常。

while (true)
{
    Group g = null;
    if (countGroupsList != null)
    {
        lock (countGroupsList.SyncRoot) // Exception here
        {
            if (groupsCounter < countGroupsList.Count)
            {
                g = (Group)countGroupsList[groupsCounter];
                groupsCounter++;
            }
         }
    }

Thread.Abort is a rude way to kill a thread. Thread.Abort是杀死线程的粗鲁方式。 But if you really want to do so you can just ignore the exception. 但是,如果您确实想这样做,则可以忽略该异常。 It's the way the thread says: Ouch, I'm dying. 这就是线程说的方式:哎呀,我快死了。

You also can catch this exception and use the Thread.Abort method in your catch block. 您还可以捕获此异常,并在catch块中使用Thread.Abort方法。 But that will prevent the thread to be killed. 但这将防止线程被杀死。

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

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