我如何在推出C#应用程序从另一个? [英] How do I launch application one from another in C#?

查看:129
本文介绍了我如何在推出C#应用程序从另一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个桌面应用程序。关闭第一应用程序之后,第一应用将启动第二应用。

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.

推荐答案

您可以使用.NET的进程类为其他人所描述的启动过程。接下来的问题是,当调用。

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

在大多数情况下,无论是使用 Form.Closing Form.Closed 事件似乎是一个容易的选择。

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

但是,如果别人可以处理该事件,并可以设置 CancelEventArgs.Cancel 为真,这可能不是这样做的正确的地方。此外, Form.Closing Form.Closed 事件将不会引发当 Application.Exit ()被调用。我不知道是否如果任何未处理的异常发生的两个事件都将得到提升。 (另外,你必须决定是否要推出的 Application.Exit()或任何未处理的异常的情况下,第二个应用程序)。

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

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

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


  1. 创建一个单独的应用程序(APP0)

  2. APP0启动程序App1

  3. APP0等待对APP与Process.WaitExit退出()

  4. APP0启动App2的,并退出本身

下面所附的示例控制台应用程序显示了一个非常简单的例子:我的示例应用程序第一次启动记事本。然后,当记事本退出时,它启动MSPAINT并退出本身。

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.

如果你想隐藏控制台,你可以设置从控制台应用程序到Windows应用程序项目属性的应用程序标签下的输出类型属性。

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.

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

这篇关于我如何在推出C#应用程序从另一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆