How to create a process internally from a Windows Service application? RRS feed

  • Question

  • I am trying to launch a process from a Windows Service of mine. But this process is getting created separately, out of the Windows Service. Therefore even if I stop the service, the process still runs. I want this to process to run inside the Windows Service of mine. This will automatically kill the process when the service stops as well. How can I achieve this? Below is my code.

     public partial class Service1 : ServiceBase
            public static Process process;
            static Thread th;
            public Service1()
            protected override void OnStart(string[] args)
                th = new Thread(startMosquitto);
            protected override void OnStop()
            static protected void startMosquitto()
                process = new Process();
                process.StartInfo.FileName = @"mosquitto.exe";
                process.StartInfo.Arguments = " -c mosquitto.conf";
                process.StartInfo.CreateNoWindow = true;
                process.StartInfo.ErrorDialog = false;
                process.StartInfo.RedirectStandardError = true;
                process.StartInfo.RedirectStandardInput = true;
                process.StartInfo.RedirectStandardOutput = true;
                process.StartInfo.UseShellExecute = false;
                process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;


    Sunday, March 5, 2017 2:04 AM

All replies

  • I am not sure what you are planning to do is possible without moving the entirety of the mosquitto.exe code into an alterante thread in the service.
    You could propably modify the service to kill the spawned process on ending, but that could get messy quickly.

    To really help you we would need to know what that programm is doing or what the real programm will be doing (asuming mosquitto.exe is just a placeholder).

    Remember to mark helpfull answers as helpfull and close threads by marking answers.

    Sunday, March 5, 2017 5:11 AM
  • Mosquitto.exe is a MQTT broker that we've got. We need to create an installer that runs this as a Windows Service. We are installing this Windows Service through WiX. This is what I'm trying to do. So this windows service should create the mosquitto.exe from the installed location when the service is started. So I want this process to run from within the windows service and not from the outside.


    Sunday, March 5, 2017 11:46 AM
  • Just read up on MQTT. Aside from it doing something on a TCP/IP Network, I do not understand it much. But it is propably not that important.
    Basically you ahve a Deamon (Service1) that fathers another Deamon (moskito.exe). And you want the child Deamon to die when it is Orphanned.

    There is no direct way I know off, to tie the Child Process life to the Parrent Process on OS level under Windows. When the parrent dies, the child can go on afaik. It is supposed to terminate itself or be terminated from outside. It is it's own process after all, as much as the explorer.exe.
    I am not certain if a process could even become a Zombie under Windows, that is how unrelated they are.

    Only idea I have would be to install Mosquitto.exe as another service that is tagged to require Service1 as requirement. That way the Windows Service Management System could force-close the Child Service when the Parrent Services shuts down.

    Remember to mark helpfull answers as helpfull and close threads by marking answers.

    Sunday, March 5, 2017 5:38 PM
  • In Windows, by default, child processes are not killed with a parent process terminates.  See:

    Destroying all child processes (and grandchildren) when the parent exits


    Microsoft Technology Center - Dallas
    My blog

    • Proposed as answer by Christopher84 Sunday, March 5, 2017 9:31 PM
    Sunday, March 5, 2017 5:53 PM