In the 'old' times it was very easy to track which method is hanging: just go to debugger, hit 'pause' button and go through stack traces.
Now however, if the problem is in the async method, this approach does not work - since the next piece of code to execute is buried somewhere in the continuation tasks (technically it does not even hang)... Is there a way for such easy debugging with tasks?
UPD.
Example:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private async void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
{
await DoHolyWar();
MessageBox.Show("Holy war complete!");
}
public static async Task DoHolyWar()
{
await DoHolyWarComplicatedDetails();
Console.WriteLine("Victory!");
}
public static async Task DoHolyWarComplicatedDetails()
{
await BurnHeretics();
}
public static Task BurnHeretics()
{
var tcs = new TaskCompletionSource<object>();
// we should have done this, but we forgot
// tcs.SetResult(null);
return tcs.Task;
}
}
Notice that if you start it and hit 'pause' you will only see that DoHolyWar method is hanging, but you will not see where exactly. While if you replace 'await's with .Wait(), and do the same you will be able to examine the hanging stack trace. With this example it is quite simple, but in a real-world application it is often quite hard to find the problem. In particular a desktop application will have events loop running on the main thread, so even if something does 'hang', and you hit 'pause' you will get no clue about what went wrong.