How do i exit a running console app?


How do i exit a running console app?



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.





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 at 16:07



App.exe run





yes exactly. Cleanup makes the whole thing more complicated yes. Is there a better way to solve this?
– Loading
Jun 29 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 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 at 17:48




4 Answers
4



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.


App.exe run


true


true


Environment.Exit(0)



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.


App.exe exit


false



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.


RegistryKey



https://docs.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");



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).



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



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.



You can use Application.Exit() method


Application.Exit()





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 at 16:06






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Opening a url is failing in Swift

Export result set on Dbeaver to CSV