When you try to get the result of the task.Result task, the main thread will be blocked until the task completes its execution (i.e. the result will be available). Use task.ContinueWith if you do not want to wait for the async operation to complete:
Task<int> task = tcs.Task; task.ContinueWith(t => { MessageBox.Show(t.Result.ToString()); });
By the way, in .NET 4.5 there is a good function to resume a paused operation when a task is completed - async methods:
private async void Form1_Load(object sender, EventArgs e) { var tcs = new TaskCompletionSource<int>(); new Thread(() => { Thread.Sleep(2000); tcs.SetResult(42); }).Start(); int result = await tcs.Task; MessageBox.Show(result.ToString()); }
This method will give control to the caller immediately after the start of waiting for the result of the task. When the result is available, the method continues execution and displays a message.
In fact, as @Servy pointed out in the comments, the async method returning void is not a good practice (for example, to handle errors), but sometimes itβs normal to use it for event handlers.
source share