Using threading causes untrappable exception

  • Thread starter Thread starter Michael Lane
  • Start date Start date
M

Michael Lane

We have developed an application which performs a series of long tasks, so
in order to keep the GUI responsive we do these tasks in a separate thread.
When run from the development environment, or run as a standalone
application on any machine with VS 2003 installed, it is fine. On any other
machine, a exception is generated at a seemingly random time during
execution. The message box simply says "Application has generated an
exception which cannot be handled."

If we do not launch these processes in a separate thread (i.e call the
method directly), the problem disappears. The very simply code we use the
create the thread is as follows:

ThreadStart activeThreadStart = new ThreadStart(dataConversion);
activeThread = new Thread(activeThreadStart);
activeThread.Start();

We have attempted to trap this error to no avail, even going so far as to
use the following:

CustomExceptionHandler eh = new CustomExceptionHandler();
Application.ThreadException += new
ThreadExceptionEventHandler(eh.OnThreadException);
Application.Run(new SummaryForm());

All target machines have framework 1.1 and MDAC 2.8. If anyone has seen this
problem, or can suggest a cause or cure, that would be great.

Michael
 
Hi Michael,

You are not accessing GUI controls from worker thread, do you?
What are you doing in worker thread?
 
Michael Lane said:
We have developed an application which performs a series of long tasks, so
in order to keep the GUI responsive we do these tasks in a separate thread.
When run from the development environment, or run as a standalone
application on any machine with VS 2003 installed, it is fine. On any other
machine, a exception is generated at a seemingly random time during
execution. The message box simply says "Application has generated an
exception which cannot be handled."

If we do not launch these processes in a separate thread (i.e call the
method directly), the problem disappears. The very simply code we use the
create the thread is as follows:

ThreadStart activeThreadStart = new ThreadStart(dataConversion);
activeThread = new Thread(activeThreadStart);
activeThread.Start();

We have attempted to trap this error to no avail, even going so far as to
use the following:

CustomExceptionHandler eh = new CustomExceptionHandler();
Application.ThreadException += new
ThreadExceptionEventHandler(eh.OnThreadException);
Application.Run(new SummaryForm());

All target machines have framework 1.1 and MDAC 2.8. If anyone has seen this
problem, or can suggest a cause or cure, that would be great.

Are you updating the UI from the other thread, by any chance? If so,
that's probably the problem.
See http://www.pobox.com/~skeet/csharp/threads/winforms.shtml

Otherwise, could you post a short but complete program which
demonstrates the problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
Thanks very much for taking the time to reply to my post.

The thread is doing data conversion tasks which basically consist of data
copying, shuffling and manipulating for the most part. Yes, I am updating
the GUI from within the worker thread - I update a label and a progress bar.
Is this a potential source of problems? Is there a workaround?

Michael



Miha Markic said:
Hi Michael,

You are not accessing GUI controls from worker thread, do you?
What are you doing in worker thread?

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

Michael Lane said:
We have developed an application which performs a series of long tasks,
so in order to keep the GUI responsive we do these tasks in a separate
thread. When run from the development environment, or run as a standalone
application on any machine with VS 2003 installed, it is fine. On any
other machine, a exception is generated at a seemingly random time during
execution. The message box simply says "Application has generated an
exception which cannot be handled."

If we do not launch these processes in a separate thread (i.e call the
method directly), the problem disappears. The very simply code we use the
create the thread is as follows:

ThreadStart activeThreadStart = new ThreadStart(dataConversion);
activeThread = new Thread(activeThreadStart);
activeThread.Start();

We have attempted to trap this error to no avail, even going so far as to
use the following:

CustomExceptionHandler eh = new CustomExceptionHandler();
Application.ThreadException += new
ThreadExceptionEventHandler(eh.OnThreadException);
Application.Run(new SummaryForm());

All target machines have framework 1.1 and MDAC 2.8. If anyone has seen
this problem, or can suggest a cause or cure, that would be great.

Michael
 
John - your first link completely explains the source of the problem and the
solution. A million thanks.

Michael
 
Ok, I see Jon's article solved it.

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

Michael Lane said:
Thanks very much for taking the time to reply to my post.

The thread is doing data conversion tasks which basically consist of data
copying, shuffling and manipulating for the most part. Yes, I am updating
the GUI from within the worker thread - I update a label and a progress
bar. Is this a potential source of problems? Is there a workaround?

Michael



Miha Markic said:
Hi Michael,

You are not accessing GUI controls from worker thread, do you?
What are you doing in worker thread?

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

Michael Lane said:
We have developed an application which performs a series of long tasks,
so in order to keep the GUI responsive we do these tasks in a separate
thread. When run from the development environment, or run as a
standalone application on any machine with VS 2003 installed, it is
fine. On any other machine, a exception is generated at a seemingly
random time during execution. The message box simply says "Application
has generated an exception which cannot be handled."

If we do not launch these processes in a separate thread (i.e call the
method directly), the problem disappears. The very simply code we use
the create the thread is as follows:

ThreadStart activeThreadStart = new ThreadStart(dataConversion);
activeThread = new Thread(activeThreadStart);
activeThread.Start();

We have attempted to trap this error to no avail, even going so far as
to use the following:

CustomExceptionHandler eh = new CustomExceptionHandler();
Application.ThreadException += new
ThreadExceptionEventHandler(eh.OnThreadException);
Application.Run(new SummaryForm());

All target machines have framework 1.1 and MDAC 2.8. If anyone has seen
this problem, or can suggest a cause or cure, that would be great.

Michael
 
Back
Top