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. IEEEXtreme 10.0 - Food Truck

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme10.0 - Food Truck 题目来源 第10届IEEE极限编程大赛 https://www.hackerrank.c ...

  2. 【PAT】1002. A+B for Polynomials (25)

    1002. A+B for Polynomials (25) This time, you are supposed to find A+B where A and B are two polynom ...

  3. day4 正则表达式(regular)

    正则(regular),要使用正则表达式需要导入Python中的re(regular正则的缩写)模块.正则表达式是对字符串的处理,我们知道,字符串中有时候包含很多我们想要提取的信息,掌握这些处理字符串 ...

  4. spring_150909_hibernate_id_table

    1.新建java工程:spring_150909_hibernate_id_table,如下图所示: 2.建DogPet实体类: package com.spring.model; import ja ...

  5. linux 驱动程序 HelloWorld

    Linux驱动可以直接编译进内核,也可以以模块的形式进行加载,前者比较复杂,本文就以模块的形式加载! vi helloi_driver.c #include <linux/init.h> ...

  6. 转:Ubuntu16.04下配置php+vscode+xdebug开发环境

    转:https://blog.csdn.net/its_my_dream/article/details/78549619 操作系统是Ubuntu16.04,首先要安装和配置php运行环境,我这里用的 ...

  7. Markdown 实现首行缩进

    把输入法由半角改为全角. 两次空格之后就能够有两个汉字的缩进. 在开头的时候,先输入这个,然后紧跟着输入文本即可.分号也不要掉.        

  8. pfring破解DNA限制

    最近因工作需要,对pf_ring进行反调试.官方下载的pf_ring转发数据包的过程中,对程序做了五分钟的限制.那么如何突破此限制.此篇博客记录一下过程,已备后用. 下载源码后进行编译,此处我们利用源 ...

  9. 【转】全面了解Mysql中的事务

    为什么要有事务? 事务广泛的运用于订单系统.银行系统等多种场景.如果有以下一个场景:A用户和B用户是银行的储户.现在A要给B转账500元.那么需要做以下几件事: 1. 检查A的账户余额>500元 ...

  10. socket的使用一

    socket概念 socket层 理解socket Socket是应用层与TCP/IP协议族通信的中间软件抽象层,它是一组接口.在设计模式中,Socket其实就是一个门面模式,它把复杂的TCP/IP协 ...