I'm playing around with TPL and have this simple program
class Program
{
static void Main(string[] args)
{
MainAsync().Wait();
}
static async Task MainAsync()
{
try
{
var tcs = new TaskCompletionSource<string>();
var canceled = tcs.TrySetCanceled();
var myCanceledTask = tcs.Task;
string result = "canceled";
if (myCanceledTask.IsCanceled)
{
result = await myCanceledTask;
}
else
{
Console.WriteLine("Couldn't cancel task.");
}
Console.WriteLine("ID: \t" + myCanceledTask.Id);
Console.WriteLine("Canceled: \t" + myCanceledTask.IsCanceled);
Console.WriteLine("Completed: \t" + myCanceledTask.IsCompleted);
Console.WriteLine("Faulted: \t" + myCanceledTask.IsFaulted);
Console.WriteLine("Status: \t" + myCanceledTask.Status);
Console.WriteLine("Result: \t" + result);
Console.ReadLine();
}
catch (Exception e)
{
var msg = "CANCELED ERROR";
throw new Exception(msg, e);
}
}
}
What's interesting to me is that, when debugging, this will loop indefinitely through exceptions on the following lines, in the following order:
result = await myCanceledTask
, then on throw new Exception(msg, e)
, then on MainAsync().Wait()
, and finally a second pause on MainAsync().Wait()
.
At this point I would expect the program to exit, but it appears to loop. I can just keep clicking Continue to loop through the various breaks above. Visual Studio never drops out of debugging mode.
Any idea why Visual Studio is behaving this way? If I run the program without debugging, I get an exception and can close the program, as expected.