Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
594 views
in Technique[技术] by (71.8m points)

.net - Do I need to dispose of a Task?

I am having fun working with System.Threading.Tasks. Many of the code samples I see, however, look something like so:

Dim lcTask = Task.Factory.StartNew(Sub() DoSomeWork())
Dim lcTaskLong = Task.Factory.StartNew(Sub() DoSomeWork(), TaskCreationOptions.LongRunning)
Task.WaitAll(lcTask, lcTaskLong)

That's the extent of the sample.
Tasks implement IDisposable, so obviously I'm supposed to dispose of them, but what if I just want to "Fire and Forget"?

If I don't dispose, will I leak threads/handles/memory/karma? Am I using tasks "wrong"? (Should just use a delegate and leave tasks alone?)

Can I dispose in a ContinueWith()? (That seems like playing Russian Roulette.)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

While the normal rule of thumb is to always call Dispose() on all IDisposable implementations, Task and Task<T> are often one case where it's better to let the finalizer take care of this.

The reason Task implements IDisposable is mainly due to an internal WaitHandle. This is required to allow task continuations to work properly, and only used when there is a continuation on a Task. If there is no continuation, the Task's Dispose method has no real effect - so in this case, it's not required.

That being said, in most cases where there is a Task continuation, it's often very, very difficult to write your code in a way that allows you to properly call Dispose(). Using statements typically do not work with Task instances, since the Task call is typically asynchronous in nature. It's very easy to dispose of the Task far too early, especially when using the using statement.

If, in your case, it's relatively simple to keep a reference to the Task and call Dispose() on it correctly, I would do so. However, if this is going to cause your logic to get much more complex, I would typically pretend that Task isn't IDisposable, and allow it to be cleaned up in the finalizer for the Task.

For more details, I'd recommend reading this thread on the MSDN forums where Stephen Toub describes why Task implements IDisposable in detail, and provides similar guidance to my suggestion above.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...