Question

Lets assume I run such a code

Task.Factory.StartNew(...).ContinueWith(...);

I don't store reference for neither of two created tasks so can I be sure that they won't be disposed before starting or at the process of executing? If yes then where do reference to these tasks are being held?

Was it helpful?

Solution

A reference to a TPL Task is held by the system under two conditions:

  1. The Task is scheduled
  2. The Task is running

Upon completion of the Task and any child tasks, the reference is thrown away. References in your code will behave as expected.


I believe you have some confusion regarding garbage collection and Dispose. This question may enlighten you.

Difference between destructor, dispose and finalize method

Destructor implicitly calls the Finalize method, they are technically same. Dispose is available with those object which implements IDisposable interface...

Should you dispose Tasks?

Stephen Toub says:

No. Don’t bother disposing of your tasks.

https://devblogs.microsoft.com/pfxteam/do-i-need-to-dispose-of-tasks/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top