Exiting a program

G

Guest

When person hits the x button in the upper right conor of the screen, my
window closes but the program is still running, how can I tell it to stop
running when the button is pressed?
 
E

Eric W

You're probably just closing the form by calling

this.Close();

when the event occurs.

Application.Exit();

Should do the trick.
 
C

Chris Tacke, eMVP

The (X) is a Minimize button on PPC. Set the Form's ShowMinimize to false
and it will become (ok) which closes it.

--
Chris Tacke
Co-founder
OpenNETCF.org
Has OpenNETCF helped you? Consider donating to support us!
http://www.opennetcf.org/donate
 
G

Guest

hello,
if you also would like to check whether the application is in memory or not,
go to settings->system->memory->running programs.

actually if you try to deploy when form1.exe is running as an example ,
vs.net gives deployment error if the application is running in background
mode.

Rifat Yavuz
Microsoft Academic Software Dev. Group, Turkey
 
G

Guest

Did what you said, now however, because my program is a infinite loop, keeps
checking a folder, when I tap ok, it doesn't do much, is there any way to
tell the project that ok was tapped and it needs to now stop running the
loop, in other words how do I check or tell my loop that the ok button (old
X) has been pressed?
 
G

Guest

The Closing event will fire for the Form but I question your overall
architecture. How is it an infinite loop? Sounds like you're not using
event-based programming, which could mean poor performance.

-Chris
 
G

Guest

It keeps looking in a folder and the loop increases everytime it finds a file
and if it doesn't find the file in a particular folder, it keeps looping
until it does, as well as sleep for 5 seconds when it can't find a new file.
 
P

Paul G. Tobey [eMVP]

Every loop in your application should have some means of exiting it cleanly
and, when it's time for the application to close, that method should always
be used. This loop is in a thread other than the UI thread? If not, it
seems like your UI will be totally unresponsive...

Paul T.
 
G

Guest

I see what you mean. What I am wondering then, is how do I tell that the
application close call has been made when the user pressed ok. If I could
tell that, then I can have my loop look out for that and when it happens to
end the applicaiton and loop.
 
P

Paul G. Tobey [eMVP]

If you weren't running your loop in the UI thread but in another thread
created for the purpose, your UI would still be responsive and you'd get the
close indication. At that point, you'd signal the other thread to leave,
the loop would exit and the thread would terminate. At that point, your
application can exit by allowing the form to finish closing or by calling
Application.Exit().

Paul T.
 
G

Guest

By different thread do you mean different function or different Form. I am
new to .net Compact porgramming so I appoligize for my lack of termology.

Greg
 
P

Paul G. Tobey [eMVP]

A "thread", in operating system parlance, is another execution context. It
has its own stack, it's own program counter, etc. It is scheduled
separately from the main thread which your user interface is using, and runs
'at the same time' as other threads in the system. It allows you to perform
some action in a separate execution context from that used to respond to
user interface operations, thus keeping the work that the thread is doing
separate from what's going on in the UI. As it happens, threads are
implemented in such a way that they execute in the context of a single
function (they can call other functions, of course), called when the thread
starts and causing the thread to exit when they exit. Yours might be
roughly something like the below:

int MyThread()
{
// The Wait delays either 5 seconds or until the exitEvent is set. In
the main thread
// (the user interface thread), when it's time to exit this thread, you
set the exitEvent
// to notify the thread to leave.
while ( WaitForSingleObject( exitEvent, 5000 /* 5 seconds between loops
*/ ) == WAIT_TIMEOUT )
{
// Do whatever you do in each loop.
}

// When the exitEvent is 'set', the loop exits, and you return from the
thread function,
// ending the thread.

return 0;
}

Paul T.
 
G

Guest

I don't understand exactly how to exit the thread in otherwords to have the
new thread stop running after the program exits. I created a new thread but
it seems to still be running when I exit because the program can't be opened
again.
 
P

Paul G. Tobey [eMVP]

Having a thread not exit won't prevent the application from running again,
but you should cleanly exit *all* threads in your application. If you've
started a worker thread, make sure that you *are* notifying it to close and
that it's set up to receive the close notification and act on it.

Paul T.
 
C

Chris Tacke, eMVP

Having a thread not exit won't prevent the application from running again,

Not quite true. If the thread is running and tha app closes, then until the
GC cleans up, your EXE will be in use. Studio will not push a new one down
and will instead say it's in use. If you just run the app on a PPC, it will
try to activate the one holding the thread and I believe appear to do
nothing. Only on a vanilla CE device will a new instance run.

-Chris
 
G

Guest

so in otherwords, i need to have the loop end when application is closing and
have the worker thread point to null?

Thank again for all the help you guys are awesome.
 

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