AppDomain.CurrentDomain.ProcessExit and cleanup

I have a simple application where I need to stop the reverse flow using the Stop () function before closing the application. The problem is that my Main () function has multiple exit points (return statements)

static void Main(string[] args)
{
/// some code
return;

// some code
return;

//// etc
}

I tried using AppDomain.CurrentDomain.ProcessExit as a place to subscribe to cleanup, but it is never called (at least when there is a background thread). Is there a way to work out a good way to implement this?

+5
source share
3 answers

You can wrap all the code in a separate method and call it from Main ():

static void Main(string[] args)
{
  DoSomething();
  TerminateThread(); // Thread.Stop() code goes here
}

static void DoSomething()
{
   /// some code
   return;

   // some code
   return;

   //// etc
}
+4
source

return; , .

+2

Application.ApplicationExit Event

MSDN :

Occurs when the application is about to shut down.

+2
source

All Articles