叫任务可以吗?运行在使用声明?

时间:2021-07-26 21:41:05

Code like this, I want to put most of the work in a Task.Run, but I am not sure if the using statement will still work as expected.

像这样的代码,我想把大部分工作放在一个任务中。运行,但是我不确定使用语句是否仍然可以正常工作。

using(MemoryStream ms = new MemoryStream())
{
    Task.Run(() => 
    { 
         /* capture ms and process the stream */ 
    }
}  // Will ms will be disposed here automatically?

Thanks.

谢谢。

2 个解决方案

#1


5  

No - The stream disposal may run before your Task has finished running. You'd be better off putting the using inside the Task if you can, or manually handling the disposal at the end of the Task.

不—流处理可以在任务完成之前运行。如果可以,最好将使用放在任务内部,或者在任务结束时手动处理处理。

var ms = new MemoryStream();
Task.Run(() =>
{
    /* capture ms and process the stream */

    // Dispose of the stream manually when you are done
    ms.Dispose();
}

Note that if you are using the memory stream outside of the Task, then you run the risk of using it after it is disposed. If you can, only use the stream inside the Task and save the results to somewhere else.

请注意,如果您使用的是任务之外的内存流,那么您在处理它之后就有使用它的风险。如果可以,只使用任务内部的流并将结果保存到其他地方。

Task.Run(() =>
{
    using(var ms = new MemoryStream())
    {
        /* capture ms and process the stream */
    }
}

#2


3  

The stream will most likely be closed and disposed of before the task has completed. This following throws a ObjectDisposedException:

在任务完成之前,流很可能被关闭并被处理。以下将抛出objectdissedexception:

using(MemoryStream ms = new MemoryStream(Encoding.ASCII.GetBytes("abc"),0,3))
{
    Task.Run(() => 
    { 
         Thread.Sleep(100);
         ms.Read(new byte[3], 0, 3);
    });
}

you could move the using statement into the task closure or you can await the task like:

您可以将using语句移到任务闭包中,或者您可以等待以下任务:

using(MemoryStream ms = new MemoryStream(Encoding.ASCII.GetBytes("abc"),0,3))
{
    await Task.Run(() => 
    { 
         Thread.Sleep(100);
         var bytes = new byte[3];
         ms.Read(bytes, 0, 3);
         Console.WriteLine(Encoding.ASCII.GetString(bytes));
    });
}

#1


5  

No - The stream disposal may run before your Task has finished running. You'd be better off putting the using inside the Task if you can, or manually handling the disposal at the end of the Task.

不—流处理可以在任务完成之前运行。如果可以,最好将使用放在任务内部,或者在任务结束时手动处理处理。

var ms = new MemoryStream();
Task.Run(() =>
{
    /* capture ms and process the stream */

    // Dispose of the stream manually when you are done
    ms.Dispose();
}

Note that if you are using the memory stream outside of the Task, then you run the risk of using it after it is disposed. If you can, only use the stream inside the Task and save the results to somewhere else.

请注意,如果您使用的是任务之外的内存流,那么您在处理它之后就有使用它的风险。如果可以,只使用任务内部的流并将结果保存到其他地方。

Task.Run(() =>
{
    using(var ms = new MemoryStream())
    {
        /* capture ms and process the stream */
    }
}

#2


3  

The stream will most likely be closed and disposed of before the task has completed. This following throws a ObjectDisposedException:

在任务完成之前,流很可能被关闭并被处理。以下将抛出objectdissedexception:

using(MemoryStream ms = new MemoryStream(Encoding.ASCII.GetBytes("abc"),0,3))
{
    Task.Run(() => 
    { 
         Thread.Sleep(100);
         ms.Read(new byte[3], 0, 3);
    });
}

you could move the using statement into the task closure or you can await the task like:

您可以将using语句移到任务闭包中,或者您可以等待以下任务:

using(MemoryStream ms = new MemoryStream(Encoding.ASCII.GetBytes("abc"),0,3))
{
    await Task.Run(() => 
    { 
         Thread.Sleep(100);
         var bytes = new byte[3];
         ms.Read(bytes, 0, 3);
         Console.WriteLine(Encoding.ASCII.GetString(bytes));
    });
}