async / await: calling an async method from a non-async method


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

Leave a Reply