Search code examples
c#servermultiple-processes

C# child process behaviour after parent crash


I have a simple yet interesting question.

I need to start a process (a node.js server) from a C# application.

I found a piece of code explaining how to start the server from within the application.

 Process p = new Process();
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.RedirectStandardOutput = true;
        p.StartInfo.RedirectStandardError = true;
        p.StartInfo.RedirectStandardInput = true;
        p.StartInfo.FileName = @"c:\node\node.exe";**//Path to node installed folder****
        string argument = "\\ bundle\main.js";
        p.StartInfo.Arguments = @argument;
        p.Start();

My question is : what happend with this process if the parent process (the C# application) crashes ? Will the child process exit/crash or will it keep running as it's a totally separate program ?

In the case it keeps running, is there a way to "link" those two processes to make sure the child exits if the parent crashes ?


Solution

  • Tried you code like as below , if you want close your process you have to use this function process.CloseMainWindow(); this will close process you started.

    public static void Main()
    {    
         System.Diagnostics.Process process = new System.Diagnostics.Process();
         System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
        startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
         startInfo.FileName = "cmd.exe";
         process.StartInfo = startInfo;
         process.Start();
         Console.ReadLine();
         process.CloseMainWindow();
         process.Close();
    }
    

    In above code i started cmd.exe and as it has its own window i code CloseMainWindow() function to close that program.

    as Cmd is separate process it will not get close when you close program which started it.

    will this process keep running if the parent crashes (before the process.CloseMainWindow();) ?

    answer to question is : Cmd process will not get closed if parent get closed before calling CloseMainWindow

    try this for checking it

    public static void Main()
        {
            System.Diagnostics.Process process = new System.Diagnostics.Process();
            System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
            startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
            startInfo.FileName = "cmd.exe";
            process.StartInfo = startInfo;
            process.Start();
            throw new Exception("test");
            Console.ReadLine();
            process.CloseMainWindow();
            process.Close();
    }
    

    in above code main program got exception and it get created before closing child process then in that case child process will run.

    if you want to close External started process forcefully when application close , than make use of Application.ApplicationExit Event, and make call to CloseMainWindow() function of external process object.