Short Walks - Using JoinableTaskFactory

November 25, 2017

While attending DDD North this year, I attended a talk on avoiding deadlocks in asynchronous programming. During this talk, I was introduced to the JoinableTaskFactory.

This became strangely useful very quickly, when I encountered a problem similar to that described here. There are a couple of solutions to this question, but the least code churn is to simply make the code synchronous; however, if you do that by simply adding


to the end of the async calls, you’re very likely to result in a deadlock.

One possible solution is to wrap the call using the JoinableTaskFactory, in the following way:

var jtf = new JoinableTaskFactory(new JoinableTaskContext());
var result = jtf.Run<DataResult>(() => \_myClass.GetDataAsync());

This allows the task to return on the same synchronisation context without causing a deadlock.


Profile picture

A blog about one man's journey through code… and some pictures of the Peak District

© Paul Michaels 2024