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

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
 
G

Guest

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
 
S

Steve B.

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
 
G

Guest

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
 
S

Steve B.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top