Running a process on application exit

  • Thread starter Thread starter Kevin
  • Start date Start date
K

Kevin

Hi,

I'm developing an application that performs an automatic upgrade. The upgrade is
in the form of a CAB file that gets downloaded onto the user's device. When the
CAB file is successfully downloaded, I want to automatically start the CAB
installation. However, the CAB file contains the application I am currently
running. I need to be able to exit the application and start the CAB file
install automatically. Here's what I'm doing:

*Currently in a dialog processing the CAB file download*
*Done with downloading; show a MessageBox.
Process p = new Process();
p.StartInfo.FileName = <full name of CAB file>;
p.Start();
Application.Exit();

Generally, this works. However, when the CAB installs, it stops on the
application EXE file because it is still in use (running). I need it to stop
running.

Is it because I am spawning a process within my application's main process and
it won't die until all other threads/processes are finished?

Any help, guidance, or clarification is greatly appreciated.

Thanks,
Kevin
 
It may be just that your app takes a little while to shutdown... as a
test you could use CERunAppAtTime to start the cab file a little later
(say a few seconds) to give your application chance to shutdown.

An alternative approach would be to build a setup dll that checks to
see if your app is running before it does any installation and if it
is then uses some mechanism to shut it down, before continuing with
the cab install.

Or one other approach could be that instead of having your app run the
cab file, have your app run a small exe that waits for your app's
process to disappear, then it runs the cab file...

Matt
 
Thanks for the response, Matt.

A strange issue I'm having is that when I run the CAB file (through the app) or
when the app crashes, its process is still running. I checked Start / Settings /
System / Memory / Running Programs, and it's not there. It's like it's a ghost
process. I didn't write the application, originally. I'm only updating it. It
was my understanding that Application.Exit() would completely shut down the
application (even though it must go through a process to do it, which might take
some small amount of time). Do you know of anything that would cause the
application to hang around?

Thanks,
Kevin
 
First of all, there is no such thing as a ghost process. The OS has just
normal old processes.

The "Running programs" applet is actually not at all related to processes.
It simply enumerates the text of all top level windows. A process can have
multiple top-le vel windows, so it would show up multiple times, or it may
have no windows, or no windows with captions, and not show at all. If you
want to know what processes are running, use the Remote Process Viewer.

Application.Exit also does not exit an application. It never has (not even
on the desktop). All it does is post a WM_QUIT message to the application's
message pump, and only when called on the UI thread. Effectively that
should cause Application.Run to return, and in most apps that will cause the
processes entry thread to exit Main. *That* act is what will typically
cause a process to die, though not always. If you have worker threads
running that are blocked in a system call (like a call to Sleep or WaitOne)
then those threads will never get scheduled for termination and your process
will not end.

To get a process to exit, you must:
1. Ensure all spawned threads exit
2. Ensure that the process thread (the one that ran Main) exits.

See, nothing about Application.Exit at all. In fact Application.Exit should
be treated just like Thread.Abort - avoid it except in the most extreme
cases. If you want the app to exit, close the Form that was passed to
Application.Run and let the execution of the app run out of Main.


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Giving back to the embedded community
http://community.OpenNETCF.com
 
Back
Top