Dangling process, when calling ShowDialog twice from Main

T

Tim_Mac

hi,
i've used jason clarks recommendations for handling unhandled
exceptions in my winforms app
(http://msdn.microsoft.com/msdnmag/issues/04/06/NET/default.aspx).

if an unhandled exception happens, i open up an 'Error-Logger' form,
which sends the exception to a web service. here is the relevant code:


[STAThread]
public static void Main()
{
try
{
SubMain();
}
catch (Exception e)
{
HandleUnhandledException(e);
}
}

public static void SubMain()
{
AppDomain.CurrentDomain.UnhandledException += new
UnhandledExceptionEventHandler(OnUnhandledException); // CLR
Application.ThreadException += new
System.Threading.ThreadExceptionEventHandler(OnGuiUnhandedException); //
Windows Forms
Application.Run(new MainForm());
}

public static void HandleUnhandledException(Object o)
{
if(o == null)
{
MessageBox.Show("An unknown error has occurred", "Error");
return;
}
Exception ex = o as Exception;
if (ex != null)
new ErrorLogger(ex).ShowDialog();
}

this approach works very well. the problem of a dangling process
occurs if the user closes the ErrorLogger form before it has finished
reporting the error. A ThreadAbortException happens (which i catch and
ignore) in ErrorLogger, and the error logger form closes. if i then
close the mainform, the process is left dangling in task manager with
no windows open.
this only happens if the user kills the errorlogger form. the
errorLogger form calls the web service on a worker thread, but i am
careful to Abort this thread in Dispose() and Form.Close() and even
when the ThreadAbortException happens.

is there any way for me to work around this? maybe the application is
creating two UI threads because ShowDialog is called twice, and only
one of those threads gets properly terminated when the application is
closed?

thanks for any help
tim
 
J

Jeffrey Tan[MSFT]

Hi Tim,

Thanks for your posting!!

Based on my understanding, you intercepted the unhandled exceptions, then
use a ErrorLogger form to send the unhandled exception information to a Web
Service. Also, in ErrorLogger form, you use a worker background thread to
send the exception information. But when you closed the ErrorLogger form
and the main form before the thread is finished sending exception
information, the process will not terminate and exist in task manager. If I
misunderstand you please feel free to tell me. Thanks

First, there will be no 2 UI threads in your application, because
Form.ShowDialog method will not create a new thread, but execute the new
created form(dialog) in your main UI thread.

Second, for this issue, I suspect that the background worker thread did not
terminate when your 2 forms are closed, so the process also exists.
Actually, in ErrorLogger form, if the user closed the form before the
background thread is terminated, it is just aborting the worker thread, I
think we should intercept ErrorLogger.Closing event, then use
BackgroundThread.Join method to wait for the worker thread to fully
terminate(Also, you may use BackgroundThread.ThreadState to determine if
the worker thread is in ThreadState.Aborted or ThreadState.Stopped state,
if not, then call Abort method twice)

For more information this, please refer to the sample code listed in
"ThreadAbortException Class" and "Thread.Abort Method" in MSDN.

Further, if you still can not figure out this issue, I suggest you paste
some sample code snippet or a simple reproduce project for us, then we can
understand and work on the issue easier.
=====================================================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
T

Tim_Mac

hi Jeffrey,
thanks for the in-depth and most helpful reply. your suggestion to
Join the worker thread in Form.Closing did the trick.
the .Net threading model seems a bit unintuitive to me. to my mind,
calling Abort() should really Abort the thread, no questions asked.
seems like you shouldn't need to call Abort twice. hopefully .Net 2.0
has a nicer threading model. i'll get around to looking at it one of
these days.

thanks again for a great reply.
tim
 
J

Jeffrey Tan[MSFT]

Hi Tim,

Thanks for your great compliment and I am glad my reply can help you. For
the thread aborting issue, I will help you to feedback to the product team,
maybe there will be a clear threading model in .Net 2.0. Anyway, if you
need further help, please feel free to tell me. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top