I am trying to understand how async/await keywords works. I have a textblock on a WPF window bind to a TextBlockContent string property and a button which trigger on 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 understood that ConfigureAwait set to false would allow me to specify that I do not want to save the Current context for the "rest" of the Task that needs to be executed after the await keyword.
After debugging i realize that when i have this code, sometime the code is running on the Main thread after the line : await GetSomeString().ConfigureAwait(false); while i specifically added the configure await.
I would have expected it to always run on a different thread that the one it was in before it entered the Task.
Can someone help me understand why?
Thank you very much
await
, you can with a custom awaiter. This is considered a bad practice, though. – noseratio