return task in async method c# code example
Example 1: how to await a task c#
private async void myEvent()
{
Task myTask = MyFunction(); // MyFunction should return a task!
var result = await myTask;
}
private void SomeFunction()
{
// some code
// .....;
//
myEvent?.Invoke(); // Invoke myEvent -> myEvent is async
Task.WaitAny(myTask);
if(myTask.isCompleted)
DoSomething();
}
Example 2: can I call an action async c#
public async Task ExecuteAsync(Func<Task> func)
{
try
{
await func();
}
finally
{
}
}