How to stop the application?

I have an application, and from this application I need to start another process, disconnecting the current application and after the process is completed, restart the application. The flow is as follows: suppose I have an app.exe application and another another.exe application, so I have to do the following:

1) Run app.exe

2) Stop / shutdown app.exe and run the other.exe file from app.exe

3) When another.exe shuts down, stop / turn off another.exe and run app.exe from other.exe

Someone please give me some idea on how to do this?

+4
source share
4 answers

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

So either:

  • "app.exe" launches
  • "app.exe" spawns "another.exe" and then exits
  • "another.exe" starts
  • "another.exe" launches "app.exe" and then exits

or

  • "monitor.exe" starts
  • "app.exe" launches
  • "app.exe" is shutting down
  • "monitor.exe" detects that "app.exe" has exited and spawns "another.exe"
  • "another.exe" starts
  • "another.exe" is shutting down
  • "monitor.exe" detects that "another.exe" has come out and spawns "app.exe"

If "app.exe" should still run "another.exe", here's the option:

  • "app.exe" launches
  • "app.exe" launches "monitor.exe" and "another.exe" and then exits
  • "another.exe" starts
  • "another.exe" is shutting down
  • "monitor.exe" detects that "another.exe" has completed, and launches "app.exe" and then exits
+5
source

Here's how to stop the current process.

Process.GetCurrentProcess().Close(); 

Of course, name it as soon as you run another.exe with

 Process.Start(...). 
+1
source

I think this is what you are looking for

 Process.Start("My Process"); Process processToClose = Process.GetProcessById(your Process ID); processToClose.Kill(); 

Do not forget to include:

 Using System.Diagnostics; 

If they don't suit your needs, check out the other methods in the Process class, I'm sure something will catch you.

+1
source

You need a third exe that stops / starts your main application. When app.exe is closed, it cannot do anything.

So try the following:

write applicationSwitcher.exe, which will help you open / close applications and switch to the main application.

0
source

Source: https://habr.com/ru/post/1309426/


All Articles