Exiting application when MinimizeBox = true

S

Shannon Lloyd

Hi,
The main form of my application has MinimizeBox set to the default
(true), which is how I want it (and, according to MS, how it should be),
but I have a particular instance where I want to force my application to
exit completely. I would have thought that Application.Exit() would have
been able to accomplish this (ie that it would have forced an exit even
if the main form had been set to minimise itself on close. However, this
seems not to be the case - Application.Exit() does not dispose of my
main form; it merely closes all windows. I've tried changing MinimizeBox
to false before calling Application.Exit(), but this appears not to work
(can this property not be changed once the form has been initialised?).
Does anyone know of a way that I can get my application to force itself
to exit while keeping MinimizeBox set to the default/true value? Is
there perhaps a way to use P/Invoke to kill the process (from within
itself)?
Thanks,
Shannon
 
D

Daniel Moth

MinimizeBox true or false makes no difference to programmatically closing a
CF application. The same rule always applies: Call the Close() method of the
main form (the form that you passed to Application.Run). As it happens
Application.Exit would also work in your case but closing the main form is
the recommended way. If you are observing different results, post a small
sample that we can look at.

Cheers
Daniel
 
S

Shannon Lloyd

Daniel said:
MinimizeBox true or false makes no difference to programmatically
closing a CF application. The same rule always applies: Call the Close()
method of the main form (the form that you passed to Application.Run).
As it happens Application.Exit would also work in your case but closing
the main form is the recommended way. If you are observing different
results, post a small sample that we can look at.

Cheers
Daniel

I worked out what the problem was: My main form (A) was spawning another
form (B), and I was using the Closed event for form B to determine
whether or not I wanted to also Close() the main form (A). However, form
B was occasionally being closed before it had finished being constructed
(ie the call to Close() was coming in B's constructor [under certain
circumstances]). This seemed to be preventing B from firing a Closed
event, so form A was never even entering the event handler B_Closed().
Not sure if this is the best solution, but I moved the code for closing
form B out of its own constructor and into a separate thread using
Form.Invoke to close the form - the thread can be started within the
constructor, but it causes the actual call to close the form to occur
after the form is finished being constructed. This seems to have solved
all the problems.
Thanks for your response.
 

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