Using async await inside void method

Viewed 18995

I have method with signature I cannot change. It should be

protected override void OnInitialize()

Using Windows 8 Metro API I need to check if file exists and read it, inside this NoSignatureChange method. Using PlainOldCSharp, I would write something like

protected override void OnInitialize()
{
  ...
  try
  {
    var file = folder.OpenFile(fileName);
    fileExists=true;
  }
  catch(FileNotFoundException)
  {
    fileExists=false
  }
}

Remember, in Windows 8 API only way to check if file exists is handling FileNotFoundException Also, in Windows 8 API all FileIO API is async, so I have only file.OpenFileAsync method.

So, the question is: How should I write this code using folder.OpenFileAsync method in Windows 8 API without changing signature of containing method

2 Answers
    public async void CalculateData()
    {
        await ProcessData();
    }

    private async Task ProcessData()
    {
        await Task.Run(() =>
        {
            //time taking process
        });
    }

Or you can use

public void CalculateData()
{
    ProcessData();
}

private async void ProcessData()
{
    await Task.Run(() =>
    {
        //time taking process
    });
}
Related