[CF 2.0] How to properly exit a console application from a method in a class ?

  • Thread starter Thread starter Steve B.
  • Start date Start date
S

Steve B.

Hi,

I'm building a console application with VS 2005.
In one method of a class, I need to exit the application properly, like
Application.Exit() does in a Windows application.

Is there any "proper" way to do this, instead of using
Process.GetCurrentProcess().Kill() ?

Thanks,
Steve
 
You need to architect your app so that when you want to exit the app, the
code running in void Main() exits that method.

-Chris
 
I've already code my app like this... I basically used this code snippet :

static void Main()
{
bool needExit = MyMethod();
if(needExit) return;

DoSomethingElse();
}

static bool MyMethod()
{
if(something)
{
return true;
}
else
{
return false;
}
}

It works fine since my call stack is not deep, but what about if the method
is "deep" into the call stack ?
I guess you will answer there is no other way that "returning" from the main
method. But what about if another thread is running ? Will the process be
correctly close ?

Thanks,
Steve
 
As long as Main gets exited, the program will shut down. As for threads,
that depends on teh CF version. If it's CF 1.0, you must manually close
them. In CF 2.0, if you set IsBackground to true, then they will die
themselves.

-Chris
 
thanks :)

As long as Main gets exited, the program will shut down. As for threads,
that depends on teh CF version. If it's CF 1.0, you must manually close
them. In CF 2.0, if you set IsBackground to true, then they will die
themselves.

-Chris
 
Back
Top