These two things seem unrelated, but they are not. This is a strange problem today.
First, set the application. dispatcherunhandledexception event in APP. XAML:
Dispatcherunhandledexception="Application_dispatcherunhandledexception_1"
Then, set E. Handled to true in the event execution method:
Private VoidApplication_dispatcherunhandledexception_1 (ObjectSender, System.Windows.Threading.DispatcherunhandledexceptioneventargsE)
{
E.Handled= True;
}
In this case, all the unprocessed exceptions in the main UI thread are eliminated.
Then, an xamlparseexception is intentionally thrown in the UI. Note that manually throwing this exception is invalid and must be thrown through a real XAML parsing error. It is also easy to create an xamlparseexception. You can use XAML to create an object that can be compiled but has runtime errors, and then display it. For example, if staticresource is used to reference a non-existent resource, or the setter of the style is used to set a non-existent attribute, the xamlparseexception can be induced.
Then execute yourProgramThe result is as follows: when the xamlparseexception exception is thrown, the application. dispatcherunhandledexception event runs, and then the exception is processed. The application does not crash. Everything works. The problem arises. If the main form is closed at this time, the application process will not end! I tested both the oldest. Net 3.0 and the latest. Net 4.5.
Fortunately, there is a solution. You can set the application. shutdownmode attribute to onmainwindowclose (you can set it directly in APP. XAML, as shown below :)
Shutdownmode="Onmainwindowclose"
I cannot figure out the cause. The default value of the application. shutdownmode attribute is onlastwindowclose, which means that the system will exit only after the last form closes the main process. When the main form is off, the main process does not exit because other forms are still displayed. Does XAML parsing create a hidden form by itself?