Re: killing a process from task manager does not fire console CTRL_CLOSE_EVENT

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance




"shiry" <shiryb@xxxxxxxxx> wrote in message
news:1135250894.371967.225920@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> What do you mean by: "install a catch handler"?
> (I'm pretty new to all this programming business, so excuse me for my
> novice questions)
>

Something like this for a console program, will catch unhandled
exceptions...

public static void DoSomethingNasty()
{
int x=0,y=1;
Console.Write((y/x).ToString()); // this will throw a "devide by zero"
exception
}
static void Main(string[] args)
{

AppDomain.CurrentDomain.UnhandledException += new
UnhandledExceptionEventHandler(GenericErrorHandler);
// Start a thread and do something bad...
Thread t = new Thread(new ThreadStart(DoSomethingNasty));
t.Start();
t.Join();
}
static void GenericErrorHandler(object sender,UnhandledExceptionEventArgs
e)
{
Console.WriteLine("Exiting...");
System.Environment.Exit(1); // terminate process and return error code
1 to parent process
}


Note that this doesn't catch process terminated by a "Kill" command!!!

Willy.


.