Refactoring Backgroundworker to async/await
<p>I've always used the Backgroundworker and I'm new to async/await keywords. Right now I'm trying to refactor some code from Backgroundworker to async/await (It's a WPF MVVM app).</p>

<p>I got a Command in my vm, which is instantiated like this: </p>

<pre><code>GenerateCommand = new DelegateCommand(GenerateHandler, CanGenerate);
</code></pre>

<p>I've seen there is an 'AsyncCommand' in the dev express lib, which will probably work for me.</p>

<p>All the handler does is register an eventhandler to the finished event and call the Generate method on a service.</p>

<pre><code>private void GenerateHandler()
{
  generatorService.GenerationFinished += OnGenerationFinished;
  generatorService.Generate(mDataFields, mGenerateFilesViewModel, mAmount);      
}
</code></pre>

<p>Then the Generate method of the service starts a BackGroundWorker:</p>

<pre><code>public void Generate(IEnumerable&lt;IDataField&gt; dataFields, IGenerateFilesViewModel generateFilesViewModel, int amount)
{
  BackgroundWorker worker = new BackgroundWorker();
  worker.DoWork += worker_DoWork;
  worker.RunWorkerAsync();
}
</code></pre>

<p>The DoWork method does the time consuming task</p>

<pre><code>private void worker_DoWork(object sender, DoWorkEventArgs e)
{
  GenerateCommand generateCommand = null;
  if (mUploadFiles)
  {
    generateCommand = new GenerateCommand(mDataFields, mOutputDirectory, mTemplateFileName, mAmount, mSelectedCollection, mSelectedAccountKey);
  }
  else
  {
    generateCommand = new GenerateCommand(mDataFields, mOutputDirectory, mTemplateFileName, mAmount);
  }

  try
  {
    generateCommand.Execute();
  }
  catch (SEHException ex)
  {
    Console.WriteLine(String.Format("Generation of files threw an exception. {0}", ex.Message));
  }
}
</code></pre>

<p>I'm unsure which method I have to mark with the async keyword now and where I have to await for my async method. I tryed to mark the worker_DoWork as async and  await the call in the Generate method, but then the Generate method has to be marked as async too in order to be awaitable in the GenerateHandler of the vm?</p>

<p>Sorry if I'm confusing you, but all this is confusing me abit...</p>

<p>thanks for the help</p>