Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
There are some bugs I can't solved in My WinForm Application.
My WinForm Application process exit suddenly in abnormal with low probability, and it can't catch any exception before exit. The code of WinForm Application contain this:
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
 AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
 Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
 Application.ApplicationExit += new EventHandler(Application_ApplicationExit);
 Application.ThreadExit += new EventHandler(Application_ThreadExit);
 AppDomain.CurrentDomain.ProcessExit += new EventHandler(CurrentDomain_ProcessExit);


In the function CurrentDomain_UnhandledException and Application_ThreadException, some logs is wrote and dump file is created, these function works if I throw exception on my own initiative. but they don't work while the WinForm exit suddenly in abnormal.

And The WinForm's Config contains this:
<?xml version="1.0"?>
<configuration>
  ...
  <runtime>
    <legacyCorruptedStateExceptionsPolicy enabled="true"/>
  </runtime>
</configuration>


The WinForm Application's code is over Hundreds of thousands rows. I'm confused, I can't find where is wrong. Could somebody please help me to find the bugs ?

What I have tried:

Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
Application.ApplicationExit += new EventHandler(Application_ApplicationExit);
Application.ThreadExit += new EventHandler(Application_ThreadExit);
AppDomain.CurrentDomain.ProcessExit += new EventHandler(CurrentDomain_ProcessExit);


<legacyCorruptedStateExceptionsPolicy enabled="true"/>
Posted
Comments
CHill60 15-Oct-20 7:11am    
Where in your application is that code that you have shared and does this happen in debug mode, release mode or both?
ZhaoChaoHui 15-Oct-20 20:44pm    
The code is wrote At the beginning of "static void Main(string[] param)", they work both debug mode and release mode.

static void Main(string[] param)
{
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
......


The bugs of exit suddenly happen in release mode, but I'm not sure whether it happen in debug mode or not.
Richard Deeming 15-Oct-20 7:17am    
If you're using async code or Tasks, you might need to subscribe to the TaskScheduler.UnobservedTaskException[^] event.

In .NET 4.0, an exception in an unobserved task would terminate the process unless this event was handled. This was changed in .NET 4.5, although there is a config switch to enable the old behaviour.
ZhaoChaoHui 15-Oct-20 23:13pm    
I have add ThrowUnobservedTaskExceptions config:
<runtime>
<legacyCorruptedStateExceptionsPolicy enabled="true"/>
<ThrowUnobservedTaskExceptions enabled="true"/>
</runtime>

This is really work while i throw exception in Tasks on my own initiative. I hope it can catch the bugs. Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900