How do I launch application one from another in C#?
Asked Answered
A

8

27

I have two desktop applications. After closing the first application, the first application will start the second application.

How do I start the second application after finishing first application?

My first application creates a separate desktop.

Acquisition answered 11/7, 2009 at 4:48 Comment(0)
P
21

You can use .NET's Process Class to start a process as other people described. Then the question is when to call.

In most cases, using either Form.Closing or Form.Closed event seems to be an easy choice.

However, if someone else can handle the event and can set CancelEventArgs.Cancel to true, this may not be the right place to do this. Also, Form.Closing and Form.Closed events will not be raised when Application.Exit() is called. I am not sure whether either of events will be raised if any unhandled exceptions occur. (Also, you have to decide whether you want to launch the second application in case of Application.Exit() or any unhandled exception).

If you really want to make sure the second application (App2) launches after the first application (App1) exited, you can play a trick:

  1. Create a separate application (App0)
  2. App0 launches App1
  3. App0 waits for App1 to exit with Process.WaitExit()
  4. App0 launches App2 and exits itself

The sample console app attached below shows a very simple case: my sample app launches the notepad first. Then, when the notepad exits, it launches mspaint and exits itself.

If you want to hide the console, you can simply set the 'Output Type' property from 'Console Application' to 'Windows Application' under 'Application' tab of Project Property.

Sample code:

using System;
using System.Diagnostics;

namespace ProcessExitSample
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {

                Process firstProc = new Process();
                firstProc.StartInfo.FileName = "notepad.exe";
                firstProc.EnableRaisingEvents = true;

                firstProc.Start();

                firstProc.WaitForExit();

                //You may want to perform different actions depending on the exit code.
                Console.WriteLine("First process exited: " + firstProc.ExitCode);

                Process secondProc = new Process();
                secondProc.StartInfo.FileName = "mspaint.exe";
                secondProc.Start();                

            }
            catch (Exception ex)
            {
                Console.WriteLine("An error occurred!!!: " + ex.Message);
                return;
            }
        }
    }
}
Perni answered 11/7, 2009 at 8:11 Comment(0)
P
26

Use the Process class when you are exiting your first application.

var p = new Process();
p.StartInfo.FileName   = "notepad.exe";  // just for example, you can use yours.
p.Start();
Percept answered 11/7, 2009 at 4:51 Comment(2)
in which event i have to start second application, whether it is form closing or form closed(first application)Acquisition
It doesn't matter (unless you cancel the close in Closing), since process is launched asynchronously, and it won't prevent your application from continuing to do whatever it is supposed to do - including shutting down.Kilbride
P
21

You can use .NET's Process Class to start a process as other people described. Then the question is when to call.

In most cases, using either Form.Closing or Form.Closed event seems to be an easy choice.

However, if someone else can handle the event and can set CancelEventArgs.Cancel to true, this may not be the right place to do this. Also, Form.Closing and Form.Closed events will not be raised when Application.Exit() is called. I am not sure whether either of events will be raised if any unhandled exceptions occur. (Also, you have to decide whether you want to launch the second application in case of Application.Exit() or any unhandled exception).

If you really want to make sure the second application (App2) launches after the first application (App1) exited, you can play a trick:

  1. Create a separate application (App0)
  2. App0 launches App1
  3. App0 waits for App1 to exit with Process.WaitExit()
  4. App0 launches App2 and exits itself

The sample console app attached below shows a very simple case: my sample app launches the notepad first. Then, when the notepad exits, it launches mspaint and exits itself.

If you want to hide the console, you can simply set the 'Output Type' property from 'Console Application' to 'Windows Application' under 'Application' tab of Project Property.

Sample code:

using System;
using System.Diagnostics;

namespace ProcessExitSample
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {

                Process firstProc = new Process();
                firstProc.StartInfo.FileName = "notepad.exe";
                firstProc.EnableRaisingEvents = true;

                firstProc.Start();

                firstProc.WaitForExit();

                //You may want to perform different actions depending on the exit code.
                Console.WriteLine("First process exited: " + firstProc.ExitCode);

                Process secondProc = new Process();
                secondProc.StartInfo.FileName = "mspaint.exe";
                secondProc.Start();                

            }
            catch (Exception ex)
            {
                Console.WriteLine("An error occurred!!!: " + ex.Message);
                return;
            }
        }
    }
}
Perni answered 11/7, 2009 at 8:11 Comment(0)
C
2

You could just shell off to it, so when you are about to exit the first app just start the second app via:

System.Diagnostics.Process.Start(@"PATH\NAME.EXE");
Conduct answered 11/7, 2009 at 4:52 Comment(0)
D
2

Use .NET's Process class.

Directoire answered 11/7, 2009 at 4:52 Comment(0)
C
0

Some sample code:

try
{
  stateMainLayout b = new stateMainLayout();
 b.Location = Screen.AllScreens[1].WorkingArea.Location;
 b.ShowDialog();
 }
catch
{
 stateMainLayout b = new stateMainLayout();
b.ShowDialog();
}
Constitutionally answered 6/3, 2014 at 9:47 Comment(0)
G
0

CSharp/PowerShell Calling Another Program and Send/Recieve Data: https://huseyincakir.wordpress.com/2014/12/23/sending-input-from-csharppowershell-to-another-program/

Gothurd answered 24/12, 2014 at 9:47 Comment(0)
R
0

in Some cases its necessary to add Working directory to your code in order to make the app works perfectly. especially when the app has dependency to DLL and other resources.

 TestProcess.StartInfo.FileName = "notepad.exe"; 
 TestProcess.StartInfo.WorkingDirectory = @"C:\\blah\blah\Directory of notepad.exe\";
 TestProcess.Start();
Ritch answered 5/3, 2019 at 16:57 Comment(0)
R
0

Here ProcName means the name of the application you want to start but it can only start system application and some other application

        public void Startapp(String ProcName)
        {
            try
            {
                Process firstProc = new Process();
                firstProc.StartInfo.FileName = ProcName;
                firstProc.EnableRaisingEvents = true;
                firstProc.Start();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
Request answered 14/6, 2019 at 9:32 Comment(1)
Could you add an explanation to your answer?Runge

© 2022 - 2024 — McMap. All rights reserved.