c# where to use async 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: c# async constructor

public class ViewModel       
{       
    public ObservableCollection<TData> Data { get; set; }       

    //static async method that behave like a constructor       
    async public static Task<ViewModel> BuildViewModelAsync()  
    {       
        ObservableCollection<TData> tmpData = await GetDataTask();  
        return new ViewModel(tmpData);
    }       

    // private constructor called by the async method
    private ViewModel(ObservableCollection<TData> Data)
    {
        this.Data = Data;   
    }
}