I know there are a million questions on this and I am trying their solutions but it does not seem to behave as I would expect.
With the following code:
await Task.Run(() => { Thread.Sleep(5000); Console.WriteLine("I'm running after Thread.Sleep"); });
Console.WriteLine("I'm running after Task.Run");
I get the output:
[ 5 seconds elapses ]
I'm running after Thread.Sleep
I'm running after Task.Run
Which is the same output I would get without using Task.Run
What I want is the output:
I'm running after Task.Run
[ 5 seconds elapses ]
I'm running after Thread.Sleep
Where execution of the 'parent' thread continues and the long-running task is executed 'in the background' in a way which does not affect the 'parent' thread.