Home > Enterprise >  Avoid capturing current execution context when using Task.Run
Avoid capturing current execution context when using Task.Run

Time:03-14

I would like to create/run a task without capturing the current execution context. Is this possible?

Consider the following sample:

private static readonly AsyncLocal<object> AsyncLocal = new AsyncLocal<object>();

[TestMethod]
public void TaskRunCapturesContext()
{
    AsyncLocal.Value = 1;
    var task = Task.Run(static async () =>
        {
            await Task.Delay(1).ConfigureAwait(false);
            Console.WriteLine(AsyncLocal.Value); // Prints "1", expected is "" <-- Problem HERE
            AsyncLocal.Value = 2;
            Console.WriteLine(AsyncLocal.Value); // Prints "2", expected is "2"
        });

    Task.WaitAll(task);
    Console.WriteLine(AsyncLocal.Value); // Prints "1", expected is "1"
}

Is it possible to make the first Console.WriteLine(AsyncLocal.Value); print "" instead of "1" (because AsyncLocal.Value is null/not initialized)?

I also played around with all overloads of the more explicit Task.Factory-method, but I couldn't find a solution. Form the source it can also be seen that Task.Run(Action) translates into a call to Task.InternalStartNew(null, action, null, ..) which explicitly passes null as state-argument, so I dont see a way I could be more explicit.

Thanks in advance

CodePudding user response:

Thanks to @canton7's comment the answer is rather simple: You can prevent the the ExecutionContext from flowing by using ExecutionContext.SuppressFlow.

Corrected above sample:

private static readonly AsyncLocal<object> AsyncLocal = new AsyncLocal<object>();
[TestMethod]
public void TaskRunNoLongerCapturesContext()
{
    AsyncLocal.Value = 1;
    using (ExecutionContext.SuppressFlow()) // <-- THIS
    {
        var task = Task.Run(static async () =>
            {
                await Task.Delay(1).ConfigureAwait(false);
                Console.WriteLine(AsyncLocal.Value); // Prints "", expected is "" <-- Hurrah
                AsyncLocal.Value = 2;
                Console.WriteLine(AsyncLocal.Value); // Prints "2", expected is "2"
            });

        Task.WaitAll(task);
    }

    Console.WriteLine(AsyncLocal.Value); // Prints "1", expected is "1"
}
  • Related