1. 启动外部程序,不等待其退出。

2. 启动外部程序,等待其退出。

3. 启动外部程序,无限等待其退出。

4. 启动外部程序,通过事件监视其退出。

// using System.Diagnostics;
private string appName = "Main.exe";
/// <summary>
/// 1. 启动外部程序,不等待其退出
/// </summary>
private void button1_Click(object sender, EventArgs e)
{
Process.Start(appName);
MessageBox.Show(String.Format("外部程序 {0} 启动完成!", this.appName), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
} /// <summary>
/// 2. 启动外部程序,等待其退出
/// </summary>
private void button2_Click(object sender, EventArgs e)
{
try
{
Process proc = Process.Start(appName);
if (proc != null)
{
proc.WaitForExit();
if (proc.HasExited)
{
MessageBox.Show(String.Format("外部程序 {0} 已经退出!", this.appName), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
// 如果外部程序没有结束运行则强行终止之。
proc.Kill();
MessageBox.Show(String.Format("外部程序 {0} 被强行终止!", this.appName), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
}
catch (ArgumentException ex)
{
MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
} /// <summary>
/// 3. 启动外部程序,无限等待其退出
/// </summary>
private void button3_Click(object sender, EventArgs e)
{
try
{
Process proc = Process.Start(appName);
if (proc != null)
{
proc.WaitForExit();
MessageBox.Show(String.Format("外部程序 {0} 已经退出!", this.appName), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (ArgumentException ex)
{
MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
} /// <summary>
/// 4. 启动外部程序,通过事件监视其退出
/// </summary>
private void button4_Click(object sender, EventArgs e)
{
try
{
//启动外部程序
Process proc = Process.Start(appName);
if (proc != null)
{
//监视进程退出
proc.EnableRaisingEvents = true;
//指定退出事件方法
proc.Exited += new EventHandler(proc_Exited);
}
}
catch (ArgumentException ex)
{
MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
} /// <summary>
///启动外部程序退出事件
/// </summary>
void proc_Exited(object sender, EventArgs e)
{
MessageBox.Show(String.Format("外部程序 {0} 已经退出!", this.appName), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
}

C#启动外部程序以及等待外部程序关闭的几种方法的更多相关文章

  1. C#winform调用外部程序,等待外部程序执行完毕才执行下面代码

    1.简单调用外部程序文件(exe文件,批处理等),只需下面一行代码即可 System.Diagnostics.Process.Start(“应用程序文件全路径”); 2.如果要等待调用外部程序执行完毕 ...

  2. Intellij IDEA实现SpringBoot项目多端口启动的两种方法

    有时候使用springboot项目时遇到这样一种情况,用一个项目需要复制很多遍进行测试,除了端口号不同以外,没有任何不同.遇到这种情况怎么办呢?这时候可以使用Intellij IDEA解决 前言 有时 ...

  3. C# 启动外部程序的几种方法

    . 启动外部程序,不等待其退出. . 启动外部程序,等待其退出. . 启动外部程序,无限等待其退出. . 启动外部程序,通过事件监视其退出. // using System.Diagnostics; ...

  4. Delphi 调用外部程序并等待其运行结束

    转自:http://blog.csdn.net/xieyunc/article/details/4140620   如何让Delphi调用外部程序并等待其运行结束 1. uses     Window ...

  5. ubuntu下启动和关闭tomcat的简单方法

    在ubuntu下面,我们安装tomcat可以有两种方式[1]用aptitude安装aptitude install tomcat6 [2]免安装版从apache tomcat 网站下载apache-t ...

  6. 在Windows 10 操作系统打开Windows Mobile 设备中心,要么双击无反应,要么正在启动后过会就关闭了

    在Windows 10 操作系统打开Windows Mobile 设备中心,要么双击无反应,要么正在启动后过会就关闭了 解决方法: 1.运行:输入services.msc进入服务 2.找到(前提你的P ...

  7. 启动kafka集群,关闭kafka集群脚本

    启动kafka集群,关闭kafka集群脚本 在$KAFKA_HOME/bin下新建如下脚本文件 start-kafka.sh #!/bin/bash BROKERS="mini41 mini ...

  8. Linux启动新进程的三种方法

    程序中,我们有时需要启动一个新的进程,来完成其他的工作.下面介绍了三种实现方法,以及这三种方法之间的区别. 1.system函数-调用shell进程,开启新进程system函数,是通过启动shell进 ...

  9. Oracle开启和关闭的四种模式

    >1 启动数据库 在cmd命令窗口,直接输入"sqlplus",直接进入oracle管理界面,输入用户名和密码后,开始启动数据库,启动数据库三个步骤:启动实例.加载数据库.打 ...

随机推荐

  1. MVC – 15.路由机制

    15.1.路由检测插件 - RouteDebug 15.2.路由约束 15.3.命名路由 15.4.验证码 15.5.ASP.NET MVC 与 三层架构 15.6.Area区域 15.6.1.尝试将 ...

  2. [你必须知道的.NET]第十九回:对象创建始末(下)

    本文将介绍以下内容: 对象的创建过程 内存分配分析 内存布局研究 接上回[第十八回:对象创建始末(上)],继续对对象创建话题的讨论>>> 2.2 托管堆的内存分配机制 引用类型的实例 ...

  3. Web APi入门之Self-Host(二)

    这篇来讲讲WebApi的自托管,WebApi可以托管到控制台/winform/服务上,并不是一定要依赖IIS才行. 1.首先新建控制台项目,在通过Nuget搜索Microsoft.AspNet.Web ...

  4. [实战]MVC5+EF6+MySql企业网盘实战(9)——编辑文件名

    写在前面 上篇文章实现了文件的下载,本篇文章将实现编辑文件名的功能. 系列文章 [EF]vs15+ef6+mysql code first方式 [实战]MVC5+EF6+MySql企业网盘实战(1) ...

  5. loadrunner中controller 中scenario-> rendezvous灰色不可用的解决方法:

    1.首先确保lr_rendezvous("login");函数添加成功  Action() { web_set_max_html_param_len("2048" ...

  6. Python画一朵花

    from mpl_toolkits.mplot3d import Axes3D from matplotlib import cm from matplotlib.ticker import Line ...

  7. express中间件的理解

    参考 :https://blog.csdn.net/huang100qi/article/details/80220012 Express中间件分为三种内置中间件.自定义中间件.第三方中间件 可以与n ...

  8. java.io 文件分类

    文件分为二进制格式和文本格式. 数据在计算机中都是以二进制的形式表现的,一般来说字节是最小的数据逻辑单位,所以也可以说数据都是以字节序列的形式表现的,不管是在内存中还是磁盘文件中.如果直接把内存中的数 ...

  9. 【51Nod 1190】最小公倍数之和 V2

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1190 \[ \begin{aligned} &\sum_{i=a ...

  10. 【BZOJ 2121】字符串游戏

    http://www.lydsy.com/JudgeOnline/problem.php?id=2121 dp,设\(f(i,j,k,l)\)表示原串i到j这个子串能否被删成第k个串的长度为l的前缀. ...