How do I catch an exception occurred in the work thread from the main thread

K

Kueishiong Tu

I have a window form application. The main thread creates
a work thread to do a web request. The work thread has the
exception mechanism in place so that if there is anything
wrong with the web request, the exception is catched in
the work thread. However it still causes the main thread
to crash. The debugger output says an
'System.ExecutionEngineException' exception ocurred in
the unknown module is not catched.
How do I catch an exception occurred in the work thread
from the main thread to prevent the application from
being crashed?
 
B

Byron Cullen

Kueishiong,

Have you enclosed your thread calls from the form application within a
try/catch block? These sounds like the calls that are throwing the
exceptions, not in your thread.

Byron Cullen
ThunderTools
 
K

Kueishiong Tu

Dear Byron:

Yes, I do. Following is my code segment that starts the
work thread. As a matter of fact, I tried to catch
System::Net::WebException,
System::ExecutionEngineException
System::Exception
but I failed. The exception does being caught in the work
thread though.

Kueishiong Tu
==========================================================
try
{
Thread * roThread = new Thread(new ThreadStart(this,
roThreadProc));
roThread->IsBackground = true;
roThread->Start();
}

catch(System::Net::WebException * pex)
{
Debug::WriteLine(pex);
Debug::WriteLine("System::Net::WebException::roThread
error");
}

catch(System::ExecutionEngineException * pex)
{
Debug::WriteLine(pex);
Debug::WriteLine
("System::ExecutionEngineException::roThread error");
}

catch(System::Exception * pex)
{
Debug::WriteLine(pex);
Debug::WriteLine("System::Exception::roThread error");
}
==========================================================
 
J

Joerg Jooss

Kueishiong Tu said:
Yes, I do. Following is my code segment that starts the
work thread. As a matter of fact, I tried to catch
System::Net::WebException,
System::ExecutionEngineException
System::Exception
but I failed. The exception does being caught in the work
thread though.
Kueishiong Tu
==========================================================
try
{
Thread * roThread = new Thread(new ThreadStart(this,
roThreadProc));
roThread->IsBackground = true;
roThread->Start();
}

catch(System::Net::WebException * pex)
{
Debug::WriteLine(pex);
Debug::WriteLine("System::Net::WebException::roThread
error");
}

catch(System::ExecutionEngineException * pex)
{
Debug::WriteLine(pex);
Debug::WriteLine
("System::ExecutionEngineException::roThread error");
}

catch(System::Exception * pex)
{
Debug::WriteLine(pex);
Debug::WriteLine("System::Exception::roThread error");
}

So roThreadProc is actually performing the WebRequest? But then you
must perform the WebRequest exception handling in roThreadProc, not in
the method where you start the new thread.

Cheers,
 
H

Herfried K. Wagner [MVP]

* "Kueishiong Tu said:
Yes, I do. Following is my code segment that starts the
work thread. As a matter of fact, I tried to catch

Are you sure this question is related to the topic of this group: .NET
Windows Forms programming?
 
K

Kueishiong Tu

So roThreadProc is actually performing the WebRequest? But then you
must perform the WebRequest exception handling in roThreadProc, not in
the method where you start the new thread.

Cheers,

Dear Joerg:
I did perform the exception handling in roThreadProc, and
the exception did being caught, but it still caused the
application to crash.

Kueishiong Tu
 
K

Kueishiong Tu

Are you sure this question is related to the topic of
this group: .NET
Windows Forms programming?

It may not be related to the .NET Windows Forms
programming superficailly, but it is an advanced
subject when you do .NET Windows Forms programming.

Kueishiong Tu
 

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