Quantcast
Channel: Are object changes inside async await methods visible after completion in any case? - Stack Overflow
Viewing all articles
Browse latest Browse all 2

Are object changes inside async await methods visible after completion in any case?

$
0
0

Are object changes inside async/await methods visible after completion in any case?

After a lot of investigation I still could not find a clear statement if an update made to an outside object from the inside of an async await method is visible to proceeding code after completion of await in any case.

The code example is some kind of a filter chain, in which each filter should see any changes done by previous filters. After that, the same object will be used for further processing.

No parallel execution of filters takes place. In addition to that, I do not want to clone objects due to performance reasons.

Please see following code example:

class Program{    private List<ICallback> _allCallbacks = new List<ICallback>();    public Program()    {        // Setup callbacks, perhaps by dependency injection        _allCallbacks.Add(new MyCallback1());        _allCallbacks.Add(new MyCallback2());    }    static async Task Main()    {        Program p = new Program();        await p.RunBusinessLogic();        Console.ReadLine();    }    private async Task RunBusinessLogic()    {        MyDto dto = new MyDto();        // Setting initial value        dto.TestProperty = "start";        // Execute all callbacks and await completion        await ExecuteCallbacks(dto).ConfigureAwait(false);        // *** Is dto.TestProperty always guaranteed to be most current value ***?        Console.WriteLine(dto.TestProperty); // start-1-2?    }    public async Task ExecuteCallbacks(MyDto source)    {        foreach (ICallback callback in _allCallbacks)        {            // Execute Callbacks one after the other, no parallel execution here            await callback.OnCallback(source).ConfigureAwait(false);        }    }    public class MyDto    {        public string TestProperty { get; set; }    }    public interface ICallback    {        public Task OnCallback(MyDto dto);    }    public class MyCallback1 : ICallback    {        public async Task OnCallback(MyDto x)        {            x.TestProperty += "-1";            await Task.CompletedTask.ConfigureAwait(false);        }    }    public class MyCallback2 : ICallback    {        public async Task OnCallback(MyDto x)        {            x.TestProperty += "-2";            await Task.CompletedTask.ConfigureAwait(false);        }    }}

Possibly related: Do async and await produce acquire and release semantics?


Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images