Sometimes it is necessary to call an async method from a method that is not marked async, for example during a synchronous version implementation of an asynchronous method.
For this, it is possible to build a synchronous task by using an asynchronous lambda function.
For example, either the following asynchronous method, taking a parameter of type string and returning a value of type string :
class TestClass { public static async Task<string> TestMethodAsync(string parameter) { ... } }
The call of the method without async can be written:
public void TestMethod { var task = Task.Run<string>(async () => await TestClass.TestMethod("a_string")); var res = task.Result; // the result is treated as usual with the tasks }
Suggestions
Animated Zoom with WPF
Installing applications using Microsoft ClickOnce technology
First steps with ANTLR4 in C #
GMini Translator: an ergonomic text translator for the Windows desktop
WPF: Developing an inflector of resource dictionary keys
C #: Get information about the caller of a method
GMini Translator 2.0: a free text translator for the Windows desktop