tags:

views:

201

answers:

3

Hi,

I have simple application when I need to stop a backround thread using Stop() function before application is closed. The problem is that my Main() function has several exit points (return statements)

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

// some code
return;

//// etc
}

I tried to use AppDomain.CurrentDomain.ProcessExit as a signle place for clean up but it is never called (at least while there is a background thread). Is there a way to work out some nice way to implement that?

+1  A: 

Change the return; calls and call a cleanup routine that also terminated the process.

Oded
+1  A: 

You can use Application.ApplicationExit Event

According to MSDN the event:

Occurs when the application is about to shut down.

Giorgi
Only if his app is window form application. For a console application this will not work.
affan
+2  A: 

You can wrap all you 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
}
Igor Korkhov
Huh.. simple and great solution, agree..
Captain Comic