Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / December 2003

Tip: Looking for answers? Try searching our database.

ProcessExit event & unhandled exception

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ales Pour - 16 Dec 2003 09:12 GMT
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
Dave - 16 Dec 2003 10:17 GMT
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?

> Hello,
>
[quoted text clipped - 9 lines]
> Many thanks,
>     Ales Pour
Ales Pour - 16 Dec 2003 11:57 GMT
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();
 }
}
}

> 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,
[quoted text clipped - 22 lines]
> > Many thanks,
> >     Ales Pour
Dave - 16 Dec 2003 20:12 GMT
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.

> Dave, thanks for your response.
>
[quoted text clipped - 78 lines]
> > > Many thanks,
> > >     Ales Pour

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.