I have a WPF appplication using Prism and I have found a extrage behaviour with Async method.
I have a class with async methods like this
public class ConfigurationManager(){
public async Task<IList<T>> LoadConfigurationAsync<T>(){
Tuple<IList<T>, bool> tuple = await LoadConfigurationItemsAsync();
return tuple.Item1;
}
private async Task<Tuple<IList<T>, bool>> LoadConfigurationItemsAsync<T>(){
await Task.Run(() =>
{
});
return new Tuple<IList<T>, bool>(configList.list, succes);
}
}
And I needed to call them in sync form because I need the results in constructor of ViewModelManager and I try to use Result because is one of the ways to get the result in sync way.
public class ViewModelManager{
private readonly ConfigurationManager _configManager;
private void LoadConfiguration(){
var config = _configManager.LoadConfigurationAsync().Result;
}
}
For my surprise this causes the application to get blocked in the Result call, I know that Result is blocking but not for always, the return line of the method never gets executed.
I tryed to call it using Task.Run
and it works
private void LoadConfiguration(){
var config = Task.Run(() => _configManager.LoadConfigurationAsync()).Result;
}
I don't know what's going on here and why calling result gets application blocked and why using Task.Run
it works. It's like calling two tasks because the method is already returning a Task
.