1

I want to open a second program from my first program and still be able to work on my first program. And another thing is to close the second program from my first. Is there a way to do this?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sean Goldfarb
  • 329
  • 6
  • 22

3 Answers3

1

For running the program:

You need using System.Diagnostics;

Process open_exe(string path)
    {
    Process to_open;
    to_open.StartInfo.FileName = path;
    to_open.Start();
    return (to_open);
    }

For closing the program:

void close_exe(Process p, bool force = false)
    {
    if(force)
        {
        p.Kill();
        }
    else
        {
        p.CloseMainWindow();
        }
    }

When calling open_exe, it returns a Process, which you can use in the close_exe function as the argument.

Addition: on the close_exe function, you can either call it by:

close_exe(process);

This will use the default value for force as false, and will not force it to close


close_exe(process, true);

This will not use the default value for force, and use true, and will thus force it to close

user3476093
  • 704
  • 2
  • 6
  • 11
0

As to launching another exe-file see the following: codeproject, stackoverflow

As to closing the application I would say that the way to close the app depends on your needs. Two ways come to my mind immediately.

  • The first is to ungracefully kill the exe as a process. You can kill any exe either by id or it's exe-name.
  • The second one is to set up a communication between two processes with the help of WCF for interprocess communication.

You can easily google about killing processes in C# as well as about setting up a communication between two processes.

Community
  • 1
  • 1
EngineerSpock
  • 2,575
  • 4
  • 35
  • 57
0

You can do it just for one line (with "using System.Diagnostics")

 Process.Start(pathToAnotherProgramm);