Threads

  • Thread starter Thread starter greg.johnsen80
  • Start date Start date
G

greg.johnsen80

I have been struggling with the following problem:

In my console application I have function1 calling function2 which in
turn uses threads to do computationally intensive tasks. Some of the
threads generate exceptions which cause the whole application to
abort. How can I avoid termination and make function1 call function2
again in case of exceptions.

Any ideas would be highly appreciated.

Thanks,
Greg
 
I have been struggling with the following problem:

In my console application I have function1 calling function2 which in
turn uses threads to do computationally intensive tasks. Some of the
threads generate exceptions which cause the whole application to
abort. How can I avoid termination and make function1 call function2
again in case of exceptions.

Any ideas would be highly appreciated.

Catch the exceptions from the root of stack of the extra threads, and
communicate the failures back to the main thread somehow.
 
I have been struggling with the following problem:

In my console application I have function1 calling function2 which in
turn uses threads to do computationally intensive tasks. Some of the
threads generate exceptions which cause the whole application to
abort. How can I avoid termination and make function1 call function2
again in case of exceptions.

Any ideas would be highly appreciated.

It seems to me that if the thread is aborted it's *toast*/gone, which can
abort the application in your case. I think the key would be to try to catch
whatever exception it is prior to the thread exception, try to clean-up and
not let the threads abort.

http://msdn2.microsoft.com/en-us/library/system.threading.threadstateexception.aspx

Maybe, threads are clashing into each other and you need to lock out
objects.

<Protecting a Code Region>

http://www.codeguru.com/csharp/.net/net_general/threads/article.php/c4609/
 
Thanks for the input! I have some clarifying questions:

Why don't the exceptions generated in function2 (where all the threads
are initiated and started) don't propagate further to funtion1?
Is there a console application equivalent of
Application.ThreadException (usually used for Windows Applications)?
AppDomain.UnhandledException is similar but can't prevent halting.

Thanks,
Greg
 
greg said:
Thanks for the input! I have some clarifying questions:

Why don't the exceptions generated in function2 (where all the threads
are initiated and started) don't propagate further to funtion1?

How could they? You don't want one thread to throw an exception (at a
completely unpredictable point) just because a thread it happened to
create threw an exception.
Is there a console application equivalent of
Application.ThreadException (usually used for Windows Applications)?
AppDomain.UnhandledException is similar but can't prevent halting.

I don't know of one, but it's not hard to write a wrapper for Thread
which includes a try/catch block.
 
Thanks for the input! I have some clarifying questions:

Why don't the exceptions generated in function2 (where all the threads
are initiated and started) don't propagate further to funtion1?
Is there a console application equivalent of
Application.ThreadException (usually used for Windows Applications)?
AppDomain.UnhandledException is similar but can't prevent halting.

Thanks,
Greg

I'm pretty sure that's how it's meant to be. If F1 called F2 and F2
raised the exception you could catch it in F1. However if F2 creates a
new thread, that thread is responsible for its exceptions.
I think you need to think about why the thread is throwing an
exception, where it is thrown and how to deal with it locally.
It would also be useful to know which exception is thrown and the bit
of code doing it :)
 
I'm pretty sure that's how it's meant to be. If F1 called F2 and F2
raised the exception you could catch it in F1.

I tried to wrap F2 in a try-catch block but it won't catch the
exception and
the program terminates.
However if F2 creates a
new thread, that thread is responsible for its exceptions.
I think you need to think about why the thread is throwing an
exception, where it is thrown and how to deal with it locally.

I throw the exception because division by infinity takes place. Can I
just call
F1 instead of throwing an exception? What about the rest of the
threads?
It would also be useful to know which exception is thrown and the bit
of code doing it :)

I should have done that :).
Here is the output:
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
 
greg said:
I tried to wrap F2 in a try-catch block but it won't catch the
exception and the program terminates.

The try/catch needs to be in the same thread that the exception is
thrown in.

At this stage it would be helpful to have a short but complete program
demonstrating the problem, rather than just talking in terms of F1 and
F2.

See http://pobox.com/~skeet/csharp/complete.html
 
greg said:
Thanks for the input! I have some clarifying questions:

Why don't the exceptions generated in function2 (where all the threads
are initiated and started) don't propagate further to funtion1?
Is there a console application equivalent of
Application.ThreadException (usually used for Windows Applications)?
AppDomain.UnhandledException is similar but can't prevent halting.

That's because they are running on different threads. The application
started up on the main/parent thread and function1 and 2 are running on that
thread, then you started proxing out creating child threads from fucntion2.

You can consider each thread and its code to be running in their own world.
They can only throw the exception in their own world, and the exception
cannot be thrown across threads, that I know about.

The best you could hope for is to have a common dump file and dump the
exception into the file. On the main thread, use a timer with an elapsed
event where it checked the state of all the threads and then read the file
if <> Thread.IsAlive, which you can dump the Thread.enum.tostring() into the
file so you'll know what thread it is by name.

You might be able to do something from the main thread. Or you can use a
SystemFileWatcher on the main thread to detect the dump file and do
something from there on the main thread.

I think you're hosed if a thread goes down leading to program termination in
this case. The best you could do is restart the program.
 
Thanks for the ideas!

Is it possible to restart a console application from a thread? Can I
use System.Diagnostics.Process.Start(Application.ExecutablePath) and
then System.Environment.Exit(-1) or
System.Windows.Forms.Application.Exit() in a thread in order to
restart the whole console application. Would that stop all the
threads?

Thanks!
 
Is it possible to restart a console application from a thread?

You can restart a completely new instance of a console application, in a
completely new console, from a thread.

Note: saying "from a thread" is superfluous. *Everything* your program
does, it does so "from a thread". Even if you never explicitly create or
use a thread, and only have a single thread, you still have that one
thread.

Now, keep in mind that when you start a new process of your console
application, it doesn't run into the same console window that the current
process is running in. You can't start a new process in your current
console until the previous one has finished, and once your previous one
has finished, it loses the ability to create a new process (obviously :) ).

So, it really depends on what you're really trying to do.

IMHO, the *correct* way to deal with this issue is to put an exception
handler around the code you wrote that could throw an exception. Either
handle the exception in a meaningful way within the thread, or provide
some mechanism to report the exception back to the main controlling
thread, as has been suggested to you already. Either way, the exception
handler needs to be in the code that is running on the thread where the
exception is generated.

As a simple example: every thread has some sort of entry point. If you
create the thread explicitly, the entry point is the method you use to
create the delegate used for the thread. If you don't create the thread
explicitly, then you're in some sort of async callback, or event handler,
or something like that and again, you've provided a delegate to some other
component for the purpose of being called an an appropriate time. Either
way, the most simple example of handling the exception in that thread
would be to put the try/catch around the code in the method used to create
that delegate.

Also note: it seems to me that there are two kinds of exceptions:
"expected" and "unexpected". Now, I may be overgeneralizing a little, but
I feel that you should only be getting "expected" exceptions. That is,
you have written code that you know, because of user input or current
state of the computer or whatever, might throw an exception. In that
case, you should be able to come up with a suitable fallback plan if and
when an exception occurs. This might be a matter of reporting the error
to the user, requiring the user to provide correct input, or simply
failing silently.

On the other hand, an exception that is "unexpected" generally reflects an
error in your own code. If you are getting exceptions for which you don't
feel there is a good fallback plan, and instead you think the right
solution is to just terminate your application and start over, well...that
suggests to me that in fact you have bugs in your code and/or design.
IMHO, it is better to fix that rather than try to come up with some kludge
that shuts everything down and restarts the application again.

Pete
 
greg said:
Thanks for the ideas!

Is it possible to restart a console application from a thread? Can I
use System.Diagnostics.Process.Start(Application.ExecutablePath) and
then System.Environment.Exit(-1) or
System.Windows.Forms.Application.Exit() in a thread in order to
restart the whole console application. Would that stop all the
threads?

Yes, you should be able to restart the Console application, although I have
never done that. I have stopped a Windows Form application, pointed back to
the executable, started an new instance of the program and exited out of the
process/program that was running.

That was using a simple Shell command in code to Shell out to the O/S to
start the program, while I was ending the current running of the program.
So, I don't see why you cannot do that with a Console application.

I suggest that you completely shutdown the application. You might want to
think about some kind a profile, if possible, pointing to where you last
left off at during your processing, on the restart. I don't know if that
will be viable for you.

Yes, you should do a controlled shutdown, where during the shutdown and
restart of the application, you check thread.state for all the threads with
Thread.IsAlive and shutdown threads that are alive. I have been in that
situation where child threads were still running when the main
thread/program was stopped. :)
 
Awesome ideas guys! I really appreciate it! What do you think of the
following approach:

When the first exception is generated by some thread causing the
application to abort, AppDomain.UnhandledExceptionEventHandler (in
main) will call Application.Restart(); and then
System.Environment.Exit(-1)?
Do I need to kill all other threads which are alive or do the
functions above do the work for me?

Thanks,
Anton
 
greg said:
Awesome ideas guys! I really appreciate it! What do you think of the
following approach:

When the first exception is generated by some thread causing the
application to abort, AppDomain.UnhandledExceptionEventHandler (in
main) will call Application.Restart(); and then
System.Environment.Exit(-1)?

The best I can tell you is try it. I have never done it.
Do I need to kill all other threads which are alive or do the
functions above do the work for me?

You should be able to do it, but those child threads will be running on
their own, because the thread you're stopping is the main/heart/parent
thread the program started up on. It would seem that you would have orphaned
child threads still running, if you didn't stop them during this restart
process on the main/heart thread. I don't know if you can get a handle on a
running thread and know that it was one of your threads on a restart.
 
Awesome ideas guys! I really appreciate it! What do you think of the
following approach:

Well, since you asked, I hate it. Sorry. :)
When the first exception is generated by some thread causing the
application to abort, AppDomain.UnhandledExceptionEventHandler (in
main) will call Application.Restart(); and then
System.Environment.Exit(-1)?
Do I need to kill all other threads which are alive or do the
functions above do the work for me?

You say that the unhandled exception causes the process to exit. So, you
should not have to do any additional work to get the process to exit. The
exception is causing it to do that already. Adding a handler to the
UnhandledExceptionEvent won't change that (the handler doesn't handle the
exception, it handles the *event* that says that the exception was
unhandled). So in your handler, just start the new process...everything
else will happen naturally.

All that said, see my other post regarding why it is that just accepting
your process terminating s not a very good solution.

Pete
 
greg said:
I tried to wrap F2 in a try-catch block but it won't catch the
exception and
the program terminates.
Since F2 uses the threads (I suppose, it starts the threads.) this wont
work. You have put the try catch in the starter method of the threads, and
there handle the exception, maybe storing some information, by wich the main
thread sees what happend and what to do.

Christof
 
Do I need to kill all other threads which are alive or do the
You should be able to do it, but those child threads will be running on
their own, because the thread you're stopping is the main/heart/parent
thread the program started up on. It would seem that you would have
orphaned child threads still running, if you didn't stop them during this
restart process on the main/heart thread. I don't know if you can get a
handle on a running thread and know that it was one of your threads on a
restart.

This ain't your father's unix. This is windows. We use CreateThread, not
fork, and exiting the process terminates all child threads. The same would
be true for pthreads, nptl, and in fact just about anything except fork.
 
Mr. Arnold said:
My father died 1992, well somewhere around there. He didn't know about
computers. However, he knew a lot about automotives.

I'm sorry about your loss. Does posting in this newsgroup help you with
your grieving?
 

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

Back
Top