简体   繁体   中英

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.

Please suggest me where can I use ResetAbort method. I'm getting that exception in marked line when call the Abort() method.

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. 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. But that will prevent the thread to be killed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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