Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# application terminates unexpectedly

Tags:

c#

.net

We run a C# console application that starts multiple threads to do work. The main function looks something like this:

try
{
    DoWork();
}
catch (Exception err)
{
    Logging.Log("Exception " + err.ToString());
}
Logging.Log("Finished");

The DoWork() function reads new jobs from a database, and spawns threads to process one work item each. Since last week, the application has started disappearing mysteriously. It disappears from the processes list and there is no entry in the event logs. The log file shows work up to a certain point: it does not log an exception, or the "Finished" line.

Any clue(s) on how a C# application can vanish like that?

EDIT: Threads are created like:

new Thread(SomeObj.StartFunc).Start();

Some of the disappearances occur when no threads are running.

P.S. We installed DebugDiag with a rule to create a crash dump whenever our program crashed. It did not create any dump files when the process disappeared.

like image 797
Andomar Avatar asked Aug 14 '09 14:08

Andomar


2 Answers

You need to have a similar catch block at the top level of the function that every thread works. If there is an uncaught exception on a thread it will kill the application, and the catch block on the main thread is not going to help.

like image 125
Grzenio Avatar answered Oct 24 '22 21:10

Grzenio


What's the identity that you're using to run the console application?

Also, you might want to use SetConsoleCtrlHandler to capture the console events. Look at this blog post for more details. We had a similar issue when the console application was run under a service account, and it would occasionally get terminated. I'm not sure if this is what you're running into. Let me know I can post some code.

UPDATE: It looks like your scenario resembles what we had experienced. In your console event handler, you need to check for the LogOff event and return true. Look at this KB article.

public static void inputHandler(ConsoleCtrl.ConsoleEvent consoleEvent)
{
   if (ConsoleEvent == ConsoleCtrl.ConsoleEvent.CtrlLogOff)
       return true;
   return false;
}
like image 23
Vasu Balakrishnan Avatar answered Oct 24 '22 19:10

Vasu Balakrishnan