Wpf cancel background worker on application outputs

In my application, I have the main windows and into it, in the frame I load the page. This page performs a long-term task when the user clicks a button. My problem is that when a lengthy task is executed and the user presses the close button on the main window, the application does not seem to end because I am debugging it in VS2008 and I see that the stop button is highlighted. If I want to finish, I have to click the stop button, the application will not stop debugging automatically when I exit the application. I thought that .NET ceases to automatically run background workers when you exit the application, but I'm not sure when I see this behavior. I tried to force and cancel the background worker on an unloaded event page with something like this:

private void Page_Unloaded(object sender, RoutedEventArgs e) { // Is the Background Worker do some work? if (My_BgWorker != null && My_BgWorker.IsBusy) { //If it supports cancellation, Cancel It if (My_BgWorker.WorkerSupportsCancellation) { // Tell the Background Worker to stop working. My_BgWorker.CancelAsync(); } } } 

but without success. After executing CancelAsync (), a few minutes after that, I see that the background work ends and RunWorkerCompleted rises, and I see that the task is completed by checking the e.Cancelled argument in the event, but after this event is called, the application continues no way out, and I don’t know what does ....

I set WorkerSupportsCancellation to true to support cancellation at the beginning.

I would appreciate all the answers. Thanks.

+1
source share
3 answers

Cancellation is not performed automatically; your code in the DoWork event DoWork should handle cancellation by checking the value of the CancellationPending property. Calling CancelAsync does not interrupt the stream, it just sets CancellationPending to true ...

For instance:

 private void bgw_DoWork(object sender, DoWorkEventArgs e) { while(!bgw.CancellationPending) { ... } } 
+4
source

I think Thomas Levesek pointed out this problem.

In the general case: somewhere, some kind of thread is still executing. You can try and find out what a stream is by stopping the debugging process (the pause button called "Break All"). At this point, the next line of code should be highlighted. In addition, you can use the Themes window (in the Debug β†’ Windows section) to see exactly which thread is still running, and where.

0
source

Perfect Thomas, installing ShutdownMode in OnMainWindowClose, as you said, solved my problem. Now the debugger stops correctly;) Many thanks for the help.

What I've done:

  <Application xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" x:Class="GParts.App" StartupUri="WinMain.xaml" ShutdownMode="OnMainWindowClose"> <...> </Application> 

Finally, I would like to make one remark for the backgroundworker in the DoWork event in case the exception is raised by some type of error: I hanlde the errors inside it with the try catch clause and in the catch that I do:

  catch (Exception ex) { e.Result = ex.Message; } 

When the background worker finishes with an exception, I want RunWorkerCompleted to detect it with e.Error and show it. So what I do in RunWorkerCompleted:

  if (e.Cancelled) { // Cancelled } else if (e.Error != null) { // Exception Thrown // Here I want to show the message that produced the exception in DoWork // event. If I set e.Result = ex.Message in DoWork event, is e.Error here // containing ex.Message? } else { // Completed); } 

Is e.Error in RunWorkerCompleted containing ex.Message?

Thanks.

-1
source

Source: https://habr.com/ru/post/1304619/


All Articles