Wiltek Ghozali

How to avoid this error, when closing my application
i have put the thread codes into try{}catch{}, but this error still shown when aborting the thread.


See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.Threading.ThreadAbortException: Thread was being aborted.
at MIPSearch.FormMain.FormMain_Closing(Object sender, CancelEventArgs e)
at System.Windows.Forms.Form.OnClosing(CancelEventArgs e)
at System.Windows.Forms.Form.WmClose(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


************** Loaded Assemblies **************
mscorlib
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.573
CodeBase: file:///c:/windows/microsoft.net/framework/v1.1.4322/mscorlib.dll
----------------------------------------
M-IPSearch
Assembly Version: 1.0.0.0
Win32 Version: 1.0.0.0
CodeBase: file:///F:/Maqna%20ASCent/Maqna%20IPSearch/bin/Release/M-IPSearch.exe
----------------------------------------
System.Windows.Forms
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.573
CodeBase: file:///c:/windows/assembly/gac/system.windows.forms/1.0.5000.0__b77a5c561934e089/system.windows.forms.dll
----------------------------------------
System
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.573
CodeBase: file:///c:/windows/assembly/gac/system/1.0.5000.0__b77a5c561934e089/system.dll
----------------------------------------
wigrtm
Assembly Version: 1.0.0.0
Win32 Version: 1.0.0.0
CodeBase: file:///F:/Maqna%20ASCent/Maqna%20IPSearch/bin/Release/wigrtm.DLL
----------------------------------------
System.Drawing
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.573
CodeBase: file:///c:/windows/assembly/gac/system.drawing/1.0.5000.0__b03f5f7f11d50a3a/system.drawing.dll
----------------------------------------
System.Data
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.573
CodeBase: file:///c:/windows/assembly/gac/system.data/1.0.5000.0__b77a5c561934e089/system.data.dll
----------------------------------------
System.Xml
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.573
CodeBase: file:///c:/windows/assembly/gac/system.xml/1.0.5000.0__b77a5c561934e089/system.xml.dll
----------------------------------------
MDE
Assembly Version: 1.0.0.0
Win32 Version: 1.0.0.0
CodeBase: file:///F:/Maqna%20ASCent/Maqna%20IPSearch/bin/Release/MDE.DLL
----------------------------------------

************** JIT Debugging **************
To enable just in time (JIT) debugging, the config file for this
application or machine (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.

For example:

<configuration>
<system.windows.forms jitDebugging="true" />
</configuration>

When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the machine
rather than being handled by this dialog.





Re: Visual C# General System.Threading.ThreadAbortException

ahmedilyas

Your application was doing something in a thread and it unexpectedly exited because you closed your application, which aborts the thread.

Whenever you abort a thread, it will by default throw the ThreadAbortException.

If you can show the code where it is throwing the error, it will help alot more :-)

Be sure you place a try { } Catch block around the area where you start your thread.






Re: Visual C# General System.Threading.ThreadAbortException

Karthikeya Pavan Kumar .B

Check this System.Threading.ThreadAbortException Class






Re: Visual C# General System.Threading.ThreadAbortException

Wiltek Ghozali

Here my code :

.....

try

{

this._thread = new Thread(new ThreadStart(ListeningThread));

this._thread.Start();

}

catch (Exception e)

{

Console.Write(e);

}

.....

on form closing :

.....

private void FormMain_Closing(object sender, CancelEventArgs e)

{

try

{

this.Cursor = Cursors.WaitCursor;

this.Text = string.Format("{0} - Closing...", this.Text);

while (Thread.CurrentThread != null)

{

try

{

Thread.CurrentThread.Abort();

}

catch (Exception e1)

{

Console.Write(e1.Message);

}

}

}

finally

{

this.Cursor = Cursors.Default;

}

}

.....






Re: Visual C# General System.Threading.ThreadAbortException

Matthew Watson

Make your thread a background thread, and then you won't need to abort it:

this._thread.IsBackground = true;

Do that before you start the thread.

However, an observation: The use of Thread.Abort() is usually a problem - we treat the use of Thread.Abort() as a bug. The proper way to do this is to have the thread check a flag periodically, and exit if the flag is set. (I usually use an EventWaitHandle for this purpose.)





Re: Visual C# General System.Threading.ThreadAbortException

Wiltek Ghozali

Hm...
I've found the solution, i used Application.ExitThread();

But, i still need solution how to terminate a Thread savely





Re: Visual C# General System.Threading.ThreadAbortException

ahmedilyas

Code Snippet

Thread.CurrentThread.Abort();

thats the problem. - dont abort the thread




Re: Visual C# General System.Threading.ThreadAbortException

Wiltek Ghozali

i think nothing we can do to avoid that exception, rite
How microsoft say about this
They must be have solution about this...





Re: Visual C# General System.Threading.ThreadAbortException

ahmedilyas

No, it's documented that calling the Abort() will throw a ThreadAbortException by default, its the behaviour of doing so. The solution is NOT to call the abort() method and let it be as is.






Re: Visual C# General System.Threading.ThreadAbortException

Peter Ritchie

Wiltek, what are you trying to do

Thread.Abort is not safe, it can interrupt lock statements and lead to deadlocks in multi-threaded code. Application.ExitThread shuts down the message loop and closes your window.






Re: Visual C# General System.Threading.ThreadAbortException

Wiltek Ghozali

I'm going to make a threading, where its function is to listen to port (UDP).

Then, when i close the form, i need to terminate the active thread (actually i can use timer, but i wont).

So, the main problem is "how to terminate the thread safely "

if the "abort" method can throw an error, so which method can i use

or what's the trick

Thanks






Re: Visual C# General System.Threading.ThreadAbortException

ahmedilyas

The "trick" I believe is to just let it be, don't abort the thread but DO close the connections you have opened and stop whatever other processing you are doing but do not kill the thread/abort it. I remember this as I had this problem a while ago and pretty much was told to not Abort the thread but stop the processing of whats happening in that thread.






Re: Visual C# General System.Threading.ThreadAbortException

Wiltek Ghozali

You right... but i've done it. If i did not terminate the thread, actually my application is not closed, caused the thread is still running. By the way, thanks for you all to advice me. It's a nice to share with you all.

Thanks.






Re: Visual C# General System.Threading.ThreadAbortException

sirjis

Use an AutoResetEvent (or ManualResetEvent, if you prefer, or if don't trust yourself to always correctly propagate the return value of WaitOne), to tell the thread that it should stop running. This is assuming that you are writing the UDP listening thread, of course. Just call the Close method of the connection when the form is closing/closed.

class UdpConnection
{
private AutoResetEvent closeSignal = new AutoResetEvent(false);
private Thread listenThread = null;
public void Open()
{
listenThread = new Thread(new ThreadStart(ListenThread));
listenThread.Name = "UdpConnection.ListenThread";
listenThread.Start();
}

public void Close()
{
if (listenThread != null)
{
closeSignal.Set(); // tell the other thread it should stop
listenThread.Join(); // wait for it to stop
// now perform any additional cleanup needed
}
else
throw new InvalidOperationException("Connection is not open");
}

private void ListenThread()
{
while (true)
{
// perform all UDP stuff you're already doing
// replace any calls to Thread.Sleep(n) with
// if (closeSignal.WaitOne(n, false))
// return;

if (closeSignal.WaitOne(0, false))
return;
}
}
}





Re: Visual C# General System.Threading.ThreadAbortException

Matthew Watson

You right... but i've done it. If i did not terminate the thread, actually my application is not closed, caused the thread is still running.


Check out my answer earlier in this thread. You have to set IsBackground for the thread so that it will be terminated when the process it is running in terminates.