Title: Task_Dispose_NotCompleted error but i don't know why
Tags: c#;task;using-statement
Question: today i was faced with a very strange error regarding Tasks. In my code i tried to use the using statement this way:
protected MyClass() {
using(var initTask = new Task(() =&gt; Init())) {
initTask.ContinueWith(result =&gt; InitCompletedCallBack(result));
initTask.Start();
protected virtual void Init() {}
private void InitCompletedCallBack(AsyncResult result) {
// Stuff
When i execute this code i get an Task_Dispose_NotComplete error. The error occures when InitCompletedCallBack has finished.
While trying arounf i changed the using to this:
using(var initTask = new Task(() =&gt; Init())) {
initTask.ContinueWith(result =&gt; InitCompletedCallBack(result));
initTask.Start();
Thread.Sleep(1000);
And that worked. However, this piece of code will be executed several times when the application starts and thus freezes the application for approximatelly 15-20 seconds. But it shows that something (inside the task?) needs at least one second to finish.
Any ideas what causes this behavior?
Thanks in advance.
When i set the time to wait to 100ms, the error occures. The first lines of the StackTrace are:
bei System.Threading.Tasks.Task.Dispose(Boolean disposing)
bei System.Threading.Tasks.Task.Dispose()
bei MyClass..ctor() in c:\Path\to\source\MyClass.cs:line 33.
Line 33 is the closing bracket of the using block.
Comment: You should not be disposing tasks anyway and also not using the Task constructor. Use Task.Run. Then, this issue just goes away.

Here is the accepted answer: Any ideas what causes this behavior?
Your code causes this behavior. You dispose a task that is not yet done. Why do you want to dispose it where you dispose it? Does the using-block make sense where you put it?
Do not dispose a task that you still rely on. Remove the using block.
Comment for this answer: Yes, i've removed the using block and this works. But i was interessted in why using the using block causes the error. I do not call the Dispose() method of initTask.
Comment for this answer: "I do not call the Dispose() method of initTask", When Using() block is end it automatically call dispose() for you. that's the whole idea of Using()!
Comment for this answer: Because the task is still running. The using block will dispose the task immediately, while your task is supposed to still run. That won't work. You cannot dispose a task *and* expect it to run in the background. Chose one.


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2015:00:11/30/15', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<c#><task><using-statement>', 'Title': "Task_Dispose_NotCompleted error but i don't know why", 'ViewCount': 93, 'id': 34001164, 'user_AboutMe': '', 'user_AccountId': 2859145.0, 'user_DisplayName': 'MrToast', 'user_Reputation': 1129.0, 'user_Views': 144.0, 'user_id': 2454734.0}}