I have following code:
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Started");
var res = GetSlowStringAsync();
res.ContinueWith(
c =>
{
Console.WriteLine("Will it crash?");
//Console.WriteLine(c.Result);
}
);
//Console.WriteLine("Press any key");
Console.ReadKey();
Console.WriteLine("Continue on the main thread");
GC.Collect();
Console.WriteLine("Memory cleared");
Thread.Sleep(10000);
}
public static async Task<string> GetSlowStringAsync()
{
await Task.Delay(2000);
throw new Exception("will you handle it?");
return "somestring";
}
}
also in App.config I added following lines:
<runtime>
<ThrowUnobservedTaskExceptions enabled ="true" />
</runtime>
I'm using visual studio 2015 14.0.23107.0 D14REL Target .Net Framework 4.6. Execute in "Start without debugging" mode.
If after question "Will it crash" press any button, then program will crash.
But if to uncomment
Console.WriteLine("Press any key");
and also execute in "Run without debugging" mode then program will not crash. Why Console.WriteLine affects way of raising exceptions?