I have a application that ataches to an Excel file (using jet 4) and fills a dataset in a background thread. This background thread uses calls a specific dll to handle the connection to the Excel file.

If the user clicks 'Cancel' from the main form then the main thread calls thread.abort followed by thread.join. However, just after thread.abort is called a messagebox pops up saying "Error 2" - no other text or title is displayed in the messagebox.

I have no idea what is causing this error - i have tried catching the threadabortexception but the messagebox pops up before this exception is caught.

I am really stumped by this...any help would be really appreciated.

Re: Common Language Runtime thread.abort causes "error 2"


I need to guess here, no real idea what your "specific dll" does or how Jet responds to thread aborts. I think the CLR generates a thread abort by throwing a SEH exception. I believe the exception code is COR_E_THREADABORTED (0x80131530). Apparently, either Jet or your "specific dll" has an SEH trap handler that catches the exception and produces the message box. Why that would produce an "Error 2" is a mystery to me, that error code almost always means "file not found".

Things to look for: check if the DLL is catching Exception. It might inappropriately obtain the error code from Marshal.GetLastWin32Error() or the GetLastError() Windows API function to come up with "2".

Re: Common Language Runtime thread.abort causes "error 2"

Thomas Danecker

you should avoid calling thread.abort because it aborts the thread at an unknown state what can lead to undesired effects (like your messagebox). Try to tell the thread that it should abort (e.g. by setting an Event, etc.) and check at an adequate position for this condition.

If you post some code of your thread, I could maybe show you how to manage "secure" thread abortion.