如何在C#中启动另一个应用程序?

时间:2022-06-07 07:28:38

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.

我的第一个应用创建一个单独的桌面

6 个解决方案

#1


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

您可以使用.NET的Process Class来启动其他人描述的流程。然后问题是什么时候打电话。

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

在大多数情况下,使用Form.Closing或Form.Closed事件似乎是一个简单的选择。

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).

但是,如果其他人可以处理该事件并且可以将CancelEventArgs.Cancel设置为true,则可能不是这样做的正确位置。此外,调用Application.Exit()时不会引发Form.Closing和Form.Closed事件。如果发生任何未处理的异常,我不确定是否会引发任何一个事件。 (另外,在Application.Exit()或任何未处理的异常情况下,您必须决定是否要启动第二个应用程序)。

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

如果你真的想确保在第一个应用程序(App1)退出后启动第二个应用程序(App2),你可以玩一个技巧:

  1. Create a separate application (App0)
  2. 创建一个单独的应用程序(App0)

  3. App0 launches App1
  4. App0启动App1

  5. App0 waits for App1 to exit with Process.WaitExit()
  6. App0等待App1退出Process.WaitExit()

  7. App0 launches App2 and exits itself
  8. App0启动App2并退出

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.

下面附带的示例控制台应用程序显示了一个非常简单的案例:我的示例应用程序首先启动记事本。然后,当记事本退出时,它会启动mspaint并退出。

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.

如果要隐藏控制台,只需将“输出类型”属性从“控制台应用程序”设置为“项目属性”的“应用程序”选项卡下的“Windows应用程序”即可。

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;
            }
        }
    }
}

#2


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

退出第一个应用程序时使用Process类。

var p = new Process();
p.StartInfo.FileName   = "notepad.exe";  // just for example, you can use yours.
p.Start();

#3


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");

#4


Use .NET's Process class.

使用.NET的Process类。

#5


Some sample code:

一些示例代码:

try
{
  stateMainLayout b = new stateMainLayout();
 b.Location = Screen.AllScreens[1].WorkingArea.Location;
 b.ShowDialog();
 }
catch
{
 stateMainLayout b = new stateMainLayout();
b.ShowDialog();
}

#6


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

CSharp / PowerShell调用另一个程序并发送/接收数据:https://huseyincakir.wordpress.com/2014/12/23/sending-input-from-csharppowershell-to-another-program/

#1


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

您可以使用.NET的Process Class来启动其他人描述的流程。然后问题是什么时候打电话。

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

在大多数情况下,使用Form.Closing或Form.Closed事件似乎是一个简单的选择。

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).

但是,如果其他人可以处理该事件并且可以将CancelEventArgs.Cancel设置为true,则可能不是这样做的正确位置。此外,调用Application.Exit()时不会引发Form.Closing和Form.Closed事件。如果发生任何未处理的异常,我不确定是否会引发任何一个事件。 (另外,在Application.Exit()或任何未处理的异常情况下,您必须决定是否要启动第二个应用程序)。

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

如果你真的想确保在第一个应用程序(App1)退出后启动第二个应用程序(App2),你可以玩一个技巧:

  1. Create a separate application (App0)
  2. 创建一个单独的应用程序(App0)

  3. App0 launches App1
  4. App0启动App1

  5. App0 waits for App1 to exit with Process.WaitExit()
  6. App0等待App1退出Process.WaitExit()

  7. App0 launches App2 and exits itself
  8. App0启动App2并退出

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.

下面附带的示例控制台应用程序显示了一个非常简单的案例:我的示例应用程序首先启动记事本。然后,当记事本退出时,它会启动mspaint并退出。

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.

如果要隐藏控制台,只需将“输出类型”属性从“控制台应用程序”设置为“项目属性”的“应用程序”选项卡下的“Windows应用程序”即可。

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;
            }
        }
    }
}

#2


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

退出第一个应用程序时使用Process类。

var p = new Process();
p.StartInfo.FileName   = "notepad.exe";  // just for example, you can use yours.
p.Start();

#3


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");

#4


Use .NET's Process class.

使用.NET的Process类。

#5


Some sample code:

一些示例代码:

try
{
  stateMainLayout b = new stateMainLayout();
 b.Location = Screen.AllScreens[1].WorkingArea.Location;
 b.ShowDialog();
 }
catch
{
 stateMainLayout b = new stateMainLayout();
b.ShowDialog();
}

#6


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

CSharp / PowerShell调用另一个程序并发送/接收数据:https://huseyincakir.wordpress.com/2014/12/23/sending-input-from-csharppowershell-to-another-program/