1

I just want to run another .exe, and close the caller.

Something like:

var info = new ProcessStartInfo
{
    WorkingDirectory = "myDirectory",
    FileName = "myFile.exe"
};
Process.Start(info);
Environment.Exit(0);

However, the child process closes at the same time as the parent process.

  • The parent is an application, it closes after clicking on a button.
  • The child is an application, it closes at the same time with the parent (unwanted).
  • The parent is an launcher.
  • The child is an application that works when you launch it manually.

I have tried other solutions but they all have the same bad behaviour:

var info = new ProcessStartInfo
{
    WorkingDirectory = "myDirectory",
    FileName = "cmd",
    Arguments = $"/C myFile.exe",
    WindowStyle = ProcessWindowStyle.Hidden
};
Process.Start(info);
Environment.Exit(0);
var thread = new Thread(() =>
{
    var info = new ProcessStartInfo
    {
        WorkingDirectory = "myDirectory",
        FileName = "myFile.exe"
    };
    Process.Start(info);
});
thread.Start();
Environment.Exit(0);
Task.Factory.StartNew(() =>
{
    var info = new ProcessStartInfo
    {
        WorkingDirectory = "myDirectory",
        FileName = "myFile.exe"
    };
    Process.Start(info);
});
Rod
  • 712
  • 2
  • 12
  • 36
  • The other process should not be terminated when the parent exits. In fact, you need to [work for it to happen](https://stackoverflow.com/q/3342941/11683). – GSerg Jul 26 '19 at 18:35
  • 3
    Are you sure it isn't actually running the second process but it exits very quickly? – ivcubr Jul 26 '19 at 18:36
  • If the child process is the problem, maybe we need to see the child process' code? Perhaps it is exiting because it has completed whatever it was supposed to do, or because it threw an error. As GSerg says, ending the parent process should have no effect. – John Wu Jul 26 '19 at 18:36
  • 1
    It's a normal behavior. You need to put it in a different thread. Don't forget to kill the other thread when you are done otherwise you will have a zombie process laying around. –  Jul 26 '19 at 18:37
  • https://stackoverflow.com/questions/16264411/implementing-independent-operations-in-c-sharp –  Jul 26 '19 at 18:42
  • Possible duplicate of [Implementing independent operations in C#](https://stackoverflow.com/questions/16264411/implementing-independent-operations-in-c-sharp) –  Jul 26 '19 at 18:42
  • 3
    @human That is not a normal behaviour. Create a new console app, paste the OP's code into the `Main` and change `myFile.exe` to `notepad.exe`. – GSerg Jul 26 '19 at 18:47
  • 4
    @Rodrigue No, it has nothing to do with threads. Ignoring all the comments that explain to you that the problem is somewhere else and only responding to one comment that agrees with you is probably not the best troubleshooting approach. – 41686d6564 stands w. Palestine Jul 26 '19 at 18:51
  • I have edited, both processes are applications that work very well when they are launched manually. – Rod Jul 26 '19 at 19:07
  • 1
    look at here for more detail usage of `Process` (`CreateNoWindow ` and `UseShellExecute ` are your keys). https://stackoverflow.com/questions/1469764/run-command-prompt-commands – Mustafa Salih ASLIM Jul 26 '19 at 19:09
  • Note that lauching an application from it's executable and running an application from another application is not the same thing. Paths can be different, for example. If you have used somehere `Directory.GetCurrentDirectory()` or you haven't fully qualified a path to a resource, relying on the *current directory* in other ways (not specifying the full path of a file, for example), the other application will crash and close immediately after it's started. This is just one of the possible hiccups – Jimi Jul 26 '19 at 19:26
  • @Rodrigue I misunderstood the question in my previous comment. Here is a SO answer I tested and I can confirm you it is working; https://stackoverflow.com/a/10072082/10864482 –  Jul 26 '19 at 20:43

1 Answers1

1

This is stupid... It just doesn't work in debug mode. It's not a bug, it's intentional (fortunately it's designed that way).

(using Rider JetBrains)

Rod
  • 712
  • 2
  • 12
  • 36
  • Thanks, I've been having the same issue while using the Rider IDE. Saved me a lot of time! Bug already exists: https://youtrack.jetbrains.com/issue/RIDER-17085 – Pieter-Jan Van Robays Sep 16 '20 at 09:47