Best Way to Close an application?

P

Peter Hurford

I have a .net 2.0 application which sits in the taskbar. When the user
right-clicks its icon they get a menu which allows them to either go
further into the application, or to quit out.

The application is designed to sit in the background all the while the
user is working.

Standard stuff.

I've now got to the stage where I'm concerned with deployment, in
particular with uninstalling the application. My uninstaller must
assume that the app could be running, so for the uninstall to be clean
the first thing I'm going to need to do is to detect this and to close
it.

Question is, what is the best way to go about this?

In "old" Windows I'd have tried to find the hWnd of the taskbar icon,
then sent a wm_ message to it, or something along those lines. But is
there a smarter way of doing this in the brave new world of .net 2?

btw, just finding and "killing" the process is not really a good option
since the application does some cleanup code on exit, which I'd rather
not bypass.

tia,
Pete
 
C

Chris Dunaway

The Process class has two methods you might find useful: Close and
CloseMainWindow. You need not kill the app.
 
P

Peter Hurford

Thanks Chris,

CloseMainWindow (called repetitively) will close any windows the app
happens to have open (it may have many modeless windows open), but it
falls short on closing the app itself.

I suspect that the "interactive" way of closing the app (right-clicking
the taskbar icon and selecting Exit from the resulting context menu)
involves sending a message other than wm_close to the application. More
likely it'll be wm_user_whatever_vs_decided_it_should_be!

I'll keep hunting, any further advice welcome.
 
P

Peter Hurford

Solved this by spinning a thread when the taskbar application starts,
which listens on a specific system event. I can give this thread
sufficient information at spin-time that it knows how to shut the
application down gracefully.

All the uninstaller does is fire the event.
 

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