0

I have many instances of a given Console application running all-at-once & want to signal a specific running Console to stop processing so I can gracefully shut it down. Most options I see KILL the Process, rather than signal it.

  1. I already have the code to successfully KILL a process.
  2. I want to gracefully signal the process to stop processing records BEFORE I shut it down
  3. The Process may be on the current machine OR on another machine

I have been researching various options, but am a bit confused. So I wont list the options I researched.

So my questions are...

Q: What are by best options to signal the Process on the same machine?
Q: What are by best options to signal the Process on the another machine?

Prisoner ZERO
  • 13,848
  • 21
  • 92
  • 137
  • This smells of an [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). Shouldn't you be asking yourself **why** it doesn't close itself? Processes do not typically run forever unless something is locking them. – Liam Feb 18 '16 at 14:33
  • @Liam I already have the code to successfully KILL a process. I want to SIGNAL the process to shut-down gracefully (because it could be processing a record) – Prisoner ZERO Feb 18 '16 at 14:35
  • Your missing my point, why do you want to shut it down at all? – Liam Feb 18 '16 at 14:35
  • Have you seen this [SO question](http://stackoverflow.com/questions/813086/can-i-send-a-ctrl-c-sigint-to-an-application-on-windows)? – Becuzz Feb 18 '16 at 14:36
  • @Liam We scale up during high demand & scale down during low demand. – Prisoner ZERO Feb 18 '16 at 14:36
  • http://pinvoke.net/default.aspx/kernel32/GenerateConsoleCtrlEvent.html – Hans Passant Feb 18 '16 at 14:47

1 Answers1

1

Ok, so what everyone is trying to get at is this.

Applications are not designed to be interfered with by external applications. This is the application space. Application A should not be able to interfere with the inner workings of Application B without using some sort of public API.

If you really want it to be able to do so, you need to get into the realms of DLL Injection, which is very hacky and not easy to attain.

If you have control over the code of both applications, I would suggest you

  1. Make the application you wish to shutdown COM visible. (You should then be able to use script or code to shut it down from an external application)

or

  1. Open a port on the application you wish to shutdown with a TCPListener class. Wait for receipt of a shutdown message, then trigger your internal shutdown command. Doing an Application.Quit(); can in effect be the same as a Process.Kill - so if you have some really important business logic that needs to be completed, you would need to wrap the Application.Quit() in your own shutdown logic method.

It would be like this:

App 1 - Starts up

App 1 - Starts the TCPListener on port 12345 App 1 - Starts doing business logic

App 2 - Connects to App1 on port 12345 App 2 - Sends shutdown command

App 1 - Detects the shutdown command and sets an internal variable to say 'No more work should be started

App 1 - Finishes its last work item, then detects that the variable is still set to 'No more work', so it shuts down, having completed its current work item.

This can only be done if you have control over the code of both application or if you are very skilled with DLL Injection and hooking of internal methods. I've done it in the past where I have injected a dll into a parent application, to create the TCPListener. However, getting it to 'wait' until the current work item is finished, while preventing new items from starting - will be the main complication.

Baaleos
  • 1,703
  • 12
  • 22
  • Note - both the options of Com visible and TCPListener work equally well for local and networked machines. Although, for the COM Visible option, you will need elevated privileges on the target machine (I think) TCPListener only requires that the receiving program has started, is allowed access to the network, and the firewall does not block connections. – Baaleos Feb 18 '16 at 15:19