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
Zoom the UI with WPF
Animated Zoom with WPF
Transform the separators of a CSV file
Localize a .NET application with locbaml - excel - csvtransformer
C #: Get information about the caller of a method
How to solve a Failure of NuGet restore (impossible to access a deleted object) from the Package Man...
How to set up a theme for the Windows Terminal prompt with Git status indication / session / error /...