Y
Y
your_mirror2016-02-19 11:27:31
Mono
your_mirror, 2016-02-19 11:27:31

Why does the "child" process of a console application close?

Hello Mono lovers and non-lovers :)
A little about the system:
Linux dev 3.2.0-4-686-pae #1 SMP Debian 3.2.41-2 i686 GNU/Linux
Mono JIT compiler version 4.2.1
At this stage I am doing an update for a program that runs a bash script with some commands and I can't understand how the process behaves with a console application or any other (for example xcalc).
For example.
I have a Game.exe GUI application that creates a process that launches the Update.exe console application. If I close the Game.exe application, then Update.exe closes the same way.
If, for example, I open some xcalc thread instead of Update.exe, then xcalc will also close if Game.exe is closed.
Questions:
1. Why is this happening?
2. How can I prevent Update.exe from closing when I close Game.exe?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Y
your_mirror, 2016-02-20
@your_mirror

Decided this way. Game.exe and Update.exe will close, but the console from Update.exe will remain open and will be an independent process.
Melz , On Windows, I did the update in a similar way, but there, when Game.exe was closed, Update.exe was not a child of it and continued to exist.

static public void Run(Version version)
    {
      ProcessStartInfo psi = new ProcessStartInfo() {
        WorkingDirectory = Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory),
        FileName = "/usr/bin/mono",
        Arguments = String.Format("Update.exe -exe \"{0}\" -ud \"{1}\" -v \"{2}\"", Process.GetCurrentProcess().ProcessName, Config.ServerUrl, version)
      };
      Process.Start(psi).WaitForExit();

      MainForm.Instance.Close();
    }

public static int Main(string[] args)
    {
      ParseArguments(args);
      String tmpFile = Path.GetTempFileName();
      File.Copy("update.sh", tmpFile, true);
      Process.Start("xterm", String.Format("-e sudo bash {0} {1} {2} {3}", tmpFile, AppName, UpdateDomain, VersionToInstall));

      return 1;
    }

M
Melz, 2016-02-20
@melz

Just tried on Win10 - works. I recently slammed a virtual machine with mono)
You almost guessed it :) There are two (known to me) ways.
1. Need to use
This way, then the child process is created with the HasExited flag . Roughly speaking, you make it clear that the process that created it died before it was created, and the system will then create it by itself.
2. Use WaitForExit

// Start the child process.
Process p = new Process();
// Redirect the error stream of the child process.
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.FileName = @"C:\windows\system32\notepad.exe";
p.Start();
// Do not wait for the child process to exit before
// reading to the end of its redirected error stream.
// p.WaitForExit();
// Read the error stream first and then wait.
//string error = p.StandardError.ReadToEnd();
p.WaitForExit();

We just tell the child to wait forever for the parent to close.
In the first case, the console application closes itself, leaving notepad open. In the second, it remains open, but when you close the notebook, it still remains open.
I don’t know how it will behave on mono)) Good luck)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question