2

A task is a promise of work.

Granted you have a very simple task, that simply prints a Hello World message to the console (as a silly, but well-known example), that gets queued up to the Thread Pool - is it possible that the Task fails to complete, due to a user for example on the Operating System level aborting the thread pool thread in question?

I'm asking this because I have a queue of work items that have a continuation that will be ran regardless of the task's outcome, that disposes some IDisposable items. It occurred to me that in the event of the above happening, the resources I used will be cleaned up at a later time by the finalizers of the IDisposable objects.

SpiritBob
  • 2,355
  • 3
  • 24
  • 62
  • `is it possible that the Task fails to complete` Well, sure. The process could terminate. And the `Task` definitely won't complete then. – mjwills Jul 19 '21 at 10:51
  • @mjwills that's fine as then everything else gets terminated as well. I'm rather concerned if only a single thread of that process gets terminated down - is that a possible scenario? – SpiritBob Jul 19 '21 at 10:53
  • 1
    Yes it is possible. – mjwills Jul 19 '21 at 11:02
  • Possible but improbable and not generally worth worrying about, because disposing resources is the least of your troubles. Terminating a thread is considered to destabilize the process (because, among other things, it will fail to release locks held by the thread and possibly corrupt the heap if a memory operation was in progress) which is why no "sane" code will choose to terminate an arbitrary thread without also terminating the process. AFAIK, there is no OS level code that will spontaneously abort a user mode thread; anyone doing it manually (hopefully) knows what they're in for. – Jeroen Mostert Jul 19 '21 at 11:59
  • Related: [Does threadpool get shared between application domains?](https://stackoverflow.com/questions/3447384/does-threadpool-get-shared-between-application-domains) – Theodor Zoulias Jul 19 '21 at 13:37

0 Answers0