all groups > dotnet clr > december 2003 >
You're in the

dotnet clr

group:

ProcessExit event & unhandled exception


Re: ProcessExit event & unhandled exception Dave
12/16/2003 4:17:29 AM
dotnet clr:
The best way to ensure that native resources get cleaned up is to wrap each
native resource in a managed class, provide a Finalizer method for each one,
and release the resource in that method (use fields to indicate the
acquired/release status so you don't try to release it twice). The Finalizer
will always get called when the system is shutting down.

In C# the Finalizer syntax is ~SomeClass(){}

I'm not sure why you are not seeing the Process.Exit event. You should see
that before the finalizers are called. Perhaps if you posted some code we
could look at it. Is this a console app?

[quoted text, click to view]

ProcessExit event & unhandled exception Ales Pour
12/16/2003 10:12:17 AM
Hello,

in my app, I need to cleanup some native resources whenever the app exits,
either gracefully or not.

So I wrote ProcessExit event handler, console event handler to catch CTRL-C
(why the ProcessExit is not called when user breaks running app woth
CTRL-C?). Now, when an unhandled exception is thrown, ProcessExit handler
does not get called; why? How can I make sure to be notified when virtual
machine is shutting down?

Many thanks,
Ales Pour

Re: ProcessExit event & unhandled exception Ales Pour
12/16/2003 12:57:10 PM
Dave, thanks for your response.

Bellow is sample code. If you pass any argument on comman line, it works ok.
If you run it with no arg, IndexOutOfRangeEception is thrown, and, on my
machine (.NET 1.1), I cannot see that ~ReleaseMe finalizer and
CurrentDomain_ProcessExit handler were invoked.
(Also, if I do not setup console event handler, CTRL-C also kills the app
without finalizer and handler get called.)

What am I doing wrong? Now, in order to make my app working somehow, I have
console event, process exit event and unhandled exception event handlers to
clean those sensitive native resources on vm exit...

Thanks,
Ales

---------------------------

namespace CleaningUp
{
class ReleaseMe
{
~ReleaseMe()
{
Console.WriteLine("~ReleaseMe");
}
}

class Class1
{
static void CurrentDomain_ProcessExit(object sender, EventArgs args)
{
Console.WriteLine("CurrentDomain_ProcessExit");
}

[STAThread]
static void Main(string[] args)
{
ReleaseMe rm = new ReleaseMe();
AppDomain.CurrentDomain.ProcessExit += new
EventHandler(CurrentDomain_ProcessExit);

args[0] += "throw exception, now!";

Console.WriteLine("press any key to exit");
Console.Read();
}
}
}


[quoted text, click to view]

Re: ProcessExit event & unhandled exception Dave
12/16/2003 2:12:33 PM
I get much the same results. The reason is that when you throw an unhandled
exception the main thread it causes an unclean termination. If you attach an
unhandled exception handler you will see it.

System.AppDomain.CurrentDomain.UnhandledException += new
UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

public static void CurrentDomain_UnhandledException(object sender,
UnhandledExceptionEventArgs e)
{
Console.WriteLine("UnhandledException. AppDomain: {1}, IsTerminating?: {0}",
e.IsTerminating,AppDomain.CurrentDomain.FriendlyName);
Exception ex = (Exception)e.ExceptionObject;
Console.WriteLine("{0}",ex.Message);
}

An abnormal termination means that you don't follow the normal shutdown
logic; things just stop. You can wrap your main with a try-catch handler so
that you avoid this.


[quoted text, click to view]

AddThis Social Bookmark Button