tags:

views:

157

answers:

4

I have an application and from this application I have to start another process by shutting down the current application and after the completion of the process again start the application. The flow is as follows, suppose I have an application app.exe, and another application another.exe, so i have to do following:

1) Start app.exe

2) Stop/shutdown app.exe and start another.exe from app.exe

3) When another.exe completes, stop/shutdown another.exe and start app.exe from another.exe

Anyone please provide me some clue of how to do it ?

+5  A: 

You need to either make "another.exe" restart "app.exe" before it exits, or have a third program monitor the first two and restart "app.exe" when "another.exe" exits.

So either:

  1. "app.exe" starts
  2. "app.exe" spawns "another.exe", and then exits
  3. "another.exe" starts
  4. "another.exe" spawns "app.exe", and then exits

or:

  1. "monitor.exe" starts
  2. "app.exe" starts
  3. "app.exe" exits
  4. "monitor.exe" detects that "app.exe" exited, and spawns "another.exe"
  5. "another.exe" starts
  6. "another.exe" exits
  7. "monitor.exe" detects that "another.exe" exited, and spawns "app.exe"

If "app.exe" should still start "another.exe", here's a variant:

  1. "app.exe" starts
  2. "app.exe" spawns "monitor.exe" and "another.exe", then exits
  3. "another.exe" starts
  4. "another.exe" exits
  5. "monitor.exe" detects that "another.exe" exited, and spawns "app.exe", then exits
Lasse V. Karlsen
+1 for the monitor.exe. This is very similar to what I was typing when I saw this answer. :-)
ydobonmai
What about if either monitored process dies unexpectedly? How will "monitor.exe" determine that the termination of the process was a failure, and not the expected normal termination? It might be that the alternate process should not start if the prior one hadn't actually completed its' tasks normally - you'd need to build some sort of check into the monitor process to look for a 'good' termination event (e.g. a zero return-code) before starting the other...
Jonners
A: 

What you need is a third exe that stops/start your main application. Once app.exe is closed it can't execute anything.

So try the following:

write a applicationSwitcher.exe that helps you to open/close applications and switch to your main application.

Simon
No i have to use only these two applications...
Praveen Sharma
A: 

Here how to stop current process

Process.GetCurrentProcess().Close();

Of course call it once you have started another.exe with

Process.Start(...).
Incognito
AS I Have mentioned "Of course call it once you have started another.exe with"...Please read the answer again ;)
Incognito
The current process starts another process and this process stops first process.
Praveen Sharma
A: 

I think this is what your looking for

Process.Start("My Process");

            Process processToClose = Process.GetProcessById(your Process ID);
            processToClose.Kill();

Remember to include:

Using System.Diagnostics;

If these dont suit your needs check out the other methods in the Process class, I'm sure something will catch your eye.

Yoda