My Technical Notes

Monday, 11 May 2015

C# - Running Asynchronous synchronously within a UI thread

Following `RunSync` method runs a method synchronously on the ThreadPool and returns the result:


private T RunSync<T>(Func<Task<T>> task)
{
    var t = Task.Run<T>(task);
    t.Wait();
    return t.Result;
}

Call it like:


RunSync(() => MyAsycMethod(arg1, arg2, ...));

No comments: