wierd exception

G

Guest

Hello,
I have a C# project with windows forms.
I sometimes get unprdictable .NET exceptions. I don't always get them, so I
can't reproduce where exactly it happens. I can generally say it happens when
closing the application (maybe in the dispose method?).
Here are 2 examples:

System.Threading.ThreadAbortException: Thread was being aborted.
at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr
wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
at System.Windows.Forms.Form.DefWndProc(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ContainerControl.WndProc(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg,
IntPtr wparam, IntPtr lparam)


System.Threading.ThreadAbortException: Thread was being aborted.
at System.Windows.Forms.UnsafeNativeMethods.SetFocus(HandleRef hWnd)
at System.Windows.Forms.ContainerControl.FocusActiveControlInternal()
at System.Windows.Forms.Form.set_Active(Boolean value)
at System.Windows.Forms.Form.WmActivate(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg,
IntPtr wparam, IntPtr lparam)

Any ideas?

thanks,
 
M

Michael A. Covington

I take it you are not, yourself, doing anything with unsafe mode or anything
unusual?

How do you close the application? Application.Exit() ? Closing the main
form? Something else?
 
G

Guest

You are correct.
I have no idea what these methods do.
I close the application using System.Environment.Exit(-1).
 
J

Jorge

Hello,
I have a C# project with windows forms.
I sometimes get unprdictable .NET exceptions. I don't always get them, so I
can't reproduce where exactly it happens. I can generally say it happens when
closing the application (maybe in the dispose method?).
Here are 2 examples:

System.Threading.ThreadAbortException: Thread was being aborted.
at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr
wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
at System.Windows.Forms.Form.DefWndProc(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ContainerControl.WndProc(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg,
IntPtr wparam, IntPtr lparam)

System.Threading.ThreadAbortException: Thread was being aborted.
at System.Windows.Forms.UnsafeNativeMethods.SetFocus(HandleRef hWnd)
at System.Windows.Forms.ContainerControl.FocusActiveControlInternal()
at System.Windows.Forms.Form.set_Active(Boolean value)
at System.Windows.Forms.Form.WmActivate(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg,
IntPtr wparam, IntPtr lparam)

Any ideas?

thanks,

Are you spawning threads in your app?
Sometimes it happens when you close an application and some threads
are still running.
 
M

Michael A. Covington

dshemesh said:
You are correct.
I have no idea what these methods do.
I close the application using System.Environment.Exit(-1).

System.Environment.Exit(code) expects you to stop all the other threads
first. In a windowed application. Application.Exit() is preferred. Is
there a good reason a windowed app needs to return an exit code?
 
G

Guest

I don't really have to return an exit code, so there is no problem for me to
use Application.Exit(). Will this stop all the running threads for me?
 
G

Guest

I do not know of any threads usage in my code. Maybe since I have a few forms
in the application some threads are opened and closed automatically (and then
one or more is not closed properly?)?
 
G

Guest

Hi Michael,
I tried using Application.Exit() and encountered the following problems:
1. After calling Application.Exit() the code continues to run. It does not
really terminate the application (I am calling Application.Exit() from the
main method of my application). I see this when in debug mode.
2. I still get sometimes the exception...

thanks,
 
G

Guest

When checking the code more thoroughly It looks something like this:

void Main {
int retval = foo();
if (retval == FAILED) {
System.Environment.Exit(-1); }
else {
System.Windows.Forms.Application.Run(new psScheduleTasks()); }
}

void foo {
Thread a;
a = new Thread(new ThreadStart(ShowWaitScreen));
a.Start();


more code...

a.abort();
return retval;
}

The exception is thrown when foo returns FAILED.
I tried using System.Windows.Forms.Application.Exit() instead of
System.Environment.Exit(-1), but the exception is still begin thrown.
 

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