none
Not able to abort/CancelAsync/kill background thread

    Question

  • We are creating thousands of background threads, also we are aborting/killing them after particular span of time

    Finally we are checking what all background threads are still alive and forcefully trying to get rid of them by aborting/killing them

    Below is the code snippet where we are struggling to abort/dispose background thread

    Also, we are successfully closing almost all the background threads however few of them are not getting killed.

    Thank you in Advance.

    if (ThreadNames.Count > 0)
                        {
                            foreach (var item in ThreadNames)
                            {
                                if (DateTime.Now > item.Value)
                                {
                                    ThreadNames.Remove(item.Key);
                                    item.Key.CancelAsync();
                                    item.Key.Dispose();
                                }
                            }
                        }

    Wednesday, April 5, 2017 2:07 PM

All replies

  • We are creating thousands of background threads, also we are aborting/killing them after particular span of time

    Finally we are checking what all background threads are still alive and forcefully trying to get rid of them by aborting/killing them


    If I was the project manager on this project, I would send the programmer/analyst who designed this project back to remedial school.

    Creating thousands of background threads is crazy. It sucks up resources needlessly since the number of active threads is limited to the number of cores in your computer. Moreover, killing threads when they exceed a time limit strongly suggests a poorly thought out design. Typically, one should be signalling a thread when it is time for it is to stop so that proper cleanup of resources can take place.

    I strongly urge you to consider a complete re-design

    Wednesday, April 5, 2017 3:29 PM
  • CancelAsync - what type is item.Key? Do realize that, in general, cancel requests are an opt in feature. When you request cancellation in most implementations that is only enforced if the item in question supports it, is currently in a state where it can honor it and is actively checking for it. For example if you attempt to cancel a Background worker component that is currently blocked waiting on a DB call then the cancel will have no effect until the call returns and the code being run looks at the cancellation flag. If that never happens then cancel won't work.

    For a true thread, the only supported way of causing it to terminate is to call Kill but that is almost never recommended except in the rare case where a thread is effectively running out of control. It is an abrupt command that may leave the resources used by the thread in an undefined state. For example if that thread code had a using statement that you were in the middle of then nothing would get disposed.

    Michael Taylor
    http://www.michaeltaylorp3.net

    Wednesday, April 5, 2017 3:43 PM
    Moderator