"System.Threading.Tasks.TaskCanceledException" occurred in WindowsBase.dll when closing the application

I have this property in my view model.

public bool IsKWH { get { return _isKwh; } set { if (value.Equals(_isKwh)) return; _isKwh = value; NotifyOfPropertyChange(() => IsKWH); } } 

Sometimes (~ 1 10 times), when I close my application, I get the following error in NotifyOfPropertyChange:

An exception like "System.Threading.Tasks.TaskCanceledException" occurred in WindowsBase.dll but was not handled in the user code

Additional Information: The task has been canceled.

I have System.Threading.Timer in my view model, which calls a webservice call to update this and many other properties.

I am using Caliburn.Micro and it looks like it started to happen when I upgraded from 1.5 to 2.0.

In any case, so that this error does not occur?

+6
source share
1 answer

It is possible that your application does not interrupt any secondary threads that it uses before the application closes, and this often causes an error message, such as the message you sent. May I suggest trying the following:

 protected override void OnClosing(System.ComponentModel.CancelEventArgs e) { // close all active threads Environment.Exit(0); } 

This should force the application to close all active threads before closing it. I remember that I had a similar problem, and this small fix fixed it. It might be worth a try, let me know if that doesn't help, and we can see what other solutions might be. Hope this helps.

+3
source

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


All Articles