Basically, there are two problems with your code:
BackgroundWorker not updated to work with async . And the whole point of async methods is that they actually return the first time they await something that is not finished, instead of locking. So, when your method returns (after await ), BackgroundWorker thinks it is complete and raises RunWorkerCompleted .BgDoWork() is an async void method. Such methods are โfire and forgetโ; you cannot wait for their completion. So, if you use your method with async understanding, you will also need to change it to the async Task method.
You said you were not looking for an alternative, but I think it can help you understand the problem, if I have provided it. Assuming that BgDoWork() should work in the background thread, and BgOnRunWorkerCompleted() should work in the user interface thread, you can use this code:
private async void Click_Button(object sender, RoutedEventArgs e) { await Task.Run((Func<Task>)BgDoWork); BgOnRunWorkerCompleted(); } private void BgOnRunWorkerCompleted() { } private async Task BgDoWork() { await Method(); }
Here Task.Run() works as an async alternative to BackgroundWorker (it launches the method in the background thread and returns Task , which can be used to wait until it actually finishes). After await in Click_Button() you will return to the user interface thread, so where BgOnRunWorkerCompleted() will be launched. Click_Button() is an async void method, and this is almost the only situation you would like to use it in: an event handler method that you don't have to wait for.
svick source share