August 15, 2016

TaskCompletionSource - Bridging the Gap Between Old and New

In the latest versions of the .NET Framework, asynchronous work is represented by the Task class. A task is similar to a future or promise in other languages. You can create one in many ways the most common being Task.Run(). The result of a task is exposed by the Task.Result property. If the work is complete, then the property immediately returns a value; otherwise, it blocks until the operation is finished. Read more

December 9, 2015

A Short and Easy Introduction to .NET's Task Class

Task.Run You can use Task.Run to schedule a delegate to run on the thread pool. The method returns a new task, and if the work is complete, the result will be available via Task.Result. If not, Task.Result will block until it is complete. 1 2 3 4 5 6 7 8 9 10 11 12 private void button_Click(object sender, EventArgs e) { Task<DateTime> task = Task.Run(() => GetDateTime()); lblDateTime.Text = task. Read more

March 25, 2015

Async/Await and Recursion

While using the new async/await keywords in C# 5.0 for the first time, I noticed an interesting aspect to how recursive methods behave when using await. For one method I was working on, Resharper notified me of a possible stack overflow exception with a “function is recursive on all paths” warning, but it didn’t fail at runtime. Instead, it continued happily calling itself with no issues. What keeps it from failing is the use of the await keyword when calling DoWorkAsync(). Read more

© Joe Buschmann 2020