0

Sorry for the bad title but i had no idea how to describe it.

I am building a console app. The general idea is that it runs permanently (and does some background stuff) when entering such a command:

App.exe run 

I then would like to terminate the app when entering

App.exe exit

How would you do this? Is there something i need to keep in mind when i want such an behaviour? Or is there a better way?

I know that args[] has all the parameters stored but how would you "find" the other running process and terminate it? As i understand it, when doing run and then exit, i just open the programm two times. So exit needs to close the running process and itself.

Loading
  • 1,098
  • 1
  • 12
  • 25
  • I assume you want to close _all_ running instances of `App.exe run`? And do you want to allow those instances to perform any cleanup before they exit? It's pretty tricky to get right. – rossipedia Jun 29 '18 at 16:07
  • yes exactly. Cleanup makes the whole thing more complicated yes. Is there a better way to solve this? – Loading Jun 29 '18 at 16:09
  • There is no standard way to do this, you'd have to talk to the running instance of the process to tell it that it needs to terminate. Whether you do so yourself or leave it up to the second instance of app.exe to find the first instance is up to you. Typically done with a named pipe. Typically it is much easier to just Process.Kill() the app. – Hans Passant Jun 29 '18 at 16:11
  • Some type of external hook like I suggested (or @SlimsGhost mentioned too) will allow you to 'cleanly' exit the first instance because you just poll that hook and then when it changes you can cleanup and exit when you want to. – ivcubr Jun 29 '18 at 17:48

4 Answers4

1

One thought I had was to use a registry key so when the first App.exe run is executed it sets the registry key to a certain value (i.e. true) and the periodically checks back to see if it is still true and if it is not then it uses Environment.Exit(0) to close down.

Now your App.exe exit would need to update that registry key to false. This seems to be a fairly compact solution as you don't have to worry about finding a certain process and killing it, in fact, neither program execution has to know about the other since it is just updating a registry key.

Another solution would be to do the same thing with a temporary text file but I would recommend the registry key option before this.

Here is some documentation on the RegistryKey class for your reference. I have used this many times in previous projects so it should work for your case.

https://learn.microsoft.com/en-us/dotnet/api/microsoft.win32.registrykey?view=netframework-4.7.1

To get you started with this option, I would manually create the key in the Software hive and then access it using the following.

RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software");
ivcubr
  • 1,988
  • 9
  • 20
  • 28
0

One way you could achieve this is by only allowing one instance of your application to run.

This question has the solutions for creating a single-instance app. When invoking the app, you could validate if there is already a running instance and close that instance (the provided question has already a solution for this).

0

Building a little on the answer from @ivcubr (and others), you have a few straightforward options:

  1. Utilize the OS to find and kill the processes (e.g. use the pid and some flavor of "kill")
  2. Use named pipes or any other type of IPC to communicate directly between instances of your application
  3. Use an external hook. This could be a registry key, any sort of filesystem file, or a database. It can be anything that your app (all instances) can poll periodically (or get notified when it changes) to see when it's time for your app instances to exit.

I think #3 is the simplest to implement, unless you already have a precedent (or another good reason) for going with option #1 or #2.

SlimsGhost
  • 2,849
  • 1
  • 10
  • 16
-1

You can use Application.Exit() method

Gautam Sharma
  • 33
  • 1
  • 13
  • So when i do App.exe run it starts the whole process.. if i want to close it i do App.exe exit, but now i only close the second instance of my process and not the first (running) process – Loading Jun 29 '18 at 16:06