3

I have a service running, the service will create a new thread that will start a process.

The process it starts will call an MSI that will essentially stop the original parent service (the service that called the process that started the msi)

As of right now, the process dies off causing the MSI to fail before finishing execution.

startInfo.FileName = "UpdateInstaller.exe";
startInfo.Arguments = "ParentServiceName.exe";
startInfo.UseShellExecute = true;
startInfo.RedirectStandardOutput = false; //Edited as per comment below. Still wont work 

new Thread(() =>
{
    Thread.CurrentThread.IsBackground = true;
    Process.Start(startInfo);
}).Start();

Any ideas on how to run this process but keep it alive, even after the parent dies off?

gpmurthy
  • 2,397
  • 19
  • 21
Dayan
  • 7,634
  • 11
  • 49
  • 76
  • You could try to start it though the command line like `"cmd UpdateInstaller ParentServiceName.exe"` or so. – AgentFire Oct 10 '13 at 06:57
  • Maybe it help to you change `Thread.CurrentThread.IsBackground = false;` – progpow Oct 10 '13 at 06:57
  • UpdateInstaller is exe file? – progpow Oct 10 '13 at 07:01
  • @StanislavAgeyev Yeah, UpdateInstaller.exe, just updated it. Thanks – Dayan Oct 10 '13 at 07:11
  • 1
    There must be a mistake in your sample code, `UseShellExecute` and `RedirectStandardOutput` cannot both be set to `true`. And if they could, it wouldn't make any sense, since output redirection doesn't work when the child process is created with `UseShellExecute` == `true`. – Fabian Schmied Oct 10 '13 at 07:27
  • 1
    @Dayan Additional question: How are you stopping the service, resp. how are you hosting your code in the service? Usually, stopping a service does not kill any child processes unless you explicitly write code for this. (E.g., see http://stackoverflow.com/questions/3342941/kill-child-process-when-parent-process-is-killed or http://stackoverflow.com/questions/18318329/kill-child-process-when-parent-nt-service-is-killed-crashed.) – Fabian Schmied Oct 10 '13 at 07:41
  • @FabianSchmied The process starts up an MSIInstaller, that MSI uninstalls the service and then installs the new version of the service. So think about it this way. AgentSetup1.0.exe get installed, New process gets created inside that Service which loads up AgentSetup2.0.exe which will uninstall the services and install the new version. Purpose of this is to get the `UpdateInstaller.exe` to be the middle man. – Dayan Oct 10 '13 at 08:04
  • 1
    I know this is a completely unproductive comment but I read that title and thought, "That's a pretty good formula for a super-hero comic book..." – Jamie - Fenrir Digital Ltd Oct 10 '13 at 13:37
  • @EvilGeniusJamie Nice one! :) – Dayan Oct 10 '13 at 22:34

1 Answers1

0

This process launched by your code will stay open after the calling code dies. Case in point - if you have a console app which will naturally exit here, the notepad process will stay open after it has 'gone':

    static void Main(string[] args)
    {
        var startInfo = new ProcessStartInfo
        {
            FileName = "notepad.exe",
            UseShellExecute = true,
            RedirectStandardOutput = false
        };

        new Thread(() =>
        {
            Thread.CurrentThread.IsBackground = true;
            Process.Start(startInfo);
        }).Start();
    }
James Harcourt
  • 6,017
  • 4
  • 22
  • 42