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

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

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

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

    // using System.Diagnostics;

    private string appName = "calc.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);

最新文章

  1. 如何为Surface Dial设备开发自定义交互功能
  2. 普林斯顿算法课第四周作业_8Puzzle
  3. Leetcode: Minimum Unique Word Abbreviation
  4. fir.im Weekly - 如何打造真正的工程师文化
  5. kindeditor-在线编辑器
  6. 在cmd窗口中运行php命令
  7. linux下使用线程锁互斥访问资源
  8. IOS从一个APP跳到另一个APP
  9. Swing(一):JFrame框架窗体
  10. HTTPS-SSL/TSL与SNI的关系以及同IP多域名虚拟主机的SSL/TSL认证
  11. Task任务
  12. Direct2D 简介
  13. JavaScript编写连连看
  14. php七牛批量删除空间内的所有文件方法
  15. python 实现wav的波形显示(时域和频域)
  16. 你不可错过的Java学习资源清单(包含社区、大牛、专栏、书籍等)
  17. PowerDesigner 中模型设计导出Excel表格
  18. softmax与多分类
  19. 用PHP发送POST请求
  20. IIS 7.5 上传文件大小限制

热门文章

  1. 【转】移动web资源整理
  2. 我的web前端之路 分享些前端的好书(转)
  3. javascript 面向对象制作坦克大战 (一)
  4. [转]sublime 使用技巧总结
  5. Stunnix C++ Obfuscator使用简介
  6. 第二百七十天 how can I 坚持
  7. 利用管道实现Shell多进程
  8. POJ 3159 Candies(差分约束,最短路)
  9. Spring入门(9)-AOP初探
  10. [iOS微博项目 - 1.6] - 自定义TabBar