I am trying to understand how async / await works. I have a text block in a WPF window that binds a StringBlockContent string property and a button that fires when I click ChangeText ().
Here is my code:
public async void ChangeText() { string mystring = await TestAsync(); this.TextBlockContent= mystring; } private async Task<string> TestAsync() { var mystring = await GetSomeString().ConfigureAwait(false); mystring = mystring + "defg"; return mystring; } private Task<string> GetSomeString() { return Task.Run(() => { return "abc"; }); }
From my readings, I realized that ConfigureAwait is set to false, allowing me to indicate that I do not want to save the current context for the "rest" of the task that should be performed after the wait keyword.
After debugging, I understand that when I have this code, sometimes the code runs in the main thread after the line: await GetSomeString (). ConfigureAwait (false); while I specifically added configure.
I would expect that he will always work in a different thread than the one in which he was before he introduced the task.
Can someone help me understand why?
Many thanks
c # wpf async-await
Kl
source share