Can .NET Task instances go out of scope during run?

前端 未结 2 737
悲&欢浪女
悲&欢浪女 2020-11-30 06:33

If I have the following block of code in a method (using .NET 4 and the Task Parallel Library):

var task = new Task(() => DoSomethingLongRunning());
task.         


        
2条回答
  •  独厮守ぢ
    2020-11-30 06:49

    The task will run to completion. Even if there aren't any other references to it (not being rooted I believe is the term), the thread pool will still hold a reference to it, and prevent it from being Garbage Collected at least (I say at least, because even after it completes, there is no guarantee that it will be Garbage Collected) until completion.

提交回复
热议问题