I’ve seen many a question on stackoverflow and other places about running a process and capturing it’s output. Using the System.Diagnostics.Process correctly is not easy and most often it’s done wrong.

Some common mistakes with System.Diagnostics.Process:

  1. Not capturing both output streams (error & output)
  2. Not redirecting input can cause applications to hang
  3. Not closing redirected input can cause applications to hang
  4. Not calling BeginOutputReadLine/BeginErrorReadLine when using events
  5. Using OutputDataReceived/ErrorDataReceived without waiting for null
  6. Not checking for null in OutputDataReceived/ErrorDataReceived handlers
  7. Forgetting to set EnableRaisingEvents = true; when using the Exited event
  8. Forgetting ErrorDialog, CreateNoWindow, or UseShellExecute settings
  9. Incorrect handling of StandardOutput or StandardError stream readers

So with this said, here are some basic guidelines:

  1. Use the OutputDataReceived/ErrorDataRecieved events NOT the StandardOutput or StandardError. This will save you a lot of headache and needless thread management.
  2. Always capture all output AND input, if you don’t plan to provide input, close the stream immediately.
  3. Your process isn’t done until it exited AND you have read all the data. OutputDataReceived CAN AND WILL be fired after a call to WaitForExit() returns. You will need wait handles for each output stream and set the wait handle once your receive (null) data.

Additional Resources:

How to use System.Diagnostics.Process correctly的更多相关文章

  1. System.Diagnostics.Process.Start的妙用

    我们经常会遇到在Winform或是WPF中点击链接或按钮打开某个指定的网址, 或者是需要打开电脑中某个指定的硬盘分区及文件夹, 甚至是"控制面板"相关的东西, 那么如何做呢? 答案 ...

  2. System.Diagnostics.Process 启动进程资源或调用外部的命令的使用

    经常看到一些程序在保存为一个txt,或者excel的文件的时候,保存完毕立即打开, 启动程序或打开文件的代码 System.Diagnostics.Process.Start(System.IO.Pa ...

  3. System.Diagnostics.Process.Star的用法

    System.Diagnostics.Process.Start(); 能做什么呢?它主要有以下几个功能: 1.打开某个链接网址(弹窗). 2.定位打开某个文件目录. 3.打开系统特殊文件夹,如“控制 ...

  4. WPF中System.Diagnostics.Process.Start的妙用

    我们经常会遇到在Winform或是WPF中点击链接或按钮打开某个指定的网址, 或者是需要打开电脑中某个指定的硬盘分区及文件夹, 甚至是"控制面板"相关的东西, 那么如何做呢? 答案 ...

  5. C# 之 System.Diagnostics.Process.Start的妙用

    经常会遇到在Winform或是WPF中点击链接或按钮打开某个指定的网址, 或者是需要打开电脑中某个指定的硬盘分区及文件夹, 甚至是"控制面板"相关的东西, 如何做呢? 方法:使用S ...

  6. C#——System.Diagnostics.Process.Start的妙用

    我们经常会遇到在Winform或是WPF中点击链接或按钮打开某个指定的网址, 或者是需要打开电脑中某个指定的硬盘分区及文件夹, 甚至是"控制面板"相关的东西, 那么如何做呢? 答案 ...

  7. System.Diagnostics.Process 执行.EXE

    分类:  C#+WINFORM 2009-04-05 21:09 459人阅读 评论(0)  收藏  举报  我们经常会遇到在Winform或是WPF中点击链接或按钮打开某个指定的网址, 或者是需要打 ...

  8. System.Diagnostics.Process 测试案例

    1.System.Diagnostics.Process 执行exe文件 创建项目,编译成功后,然后把要运行的exe文件拷贝到该项目的运行工作目录下即可,代码如下: using System; usi ...

  9. (转)C# System.Diagnostics.Process.Start使用

    经常会遇到在Winform或是WPF中点击链接或按钮打开某个指定的网址, 或者是需要打开电脑中某个指定的硬盘分区及文件夹, 甚至是"控制面板"相关的东西, 如何做呢?  方法:使用 ...

随机推荐

  1. 如何选择NoSql数据库

    How to choose a No Sql database 介绍了一下怎么选择一个No Sql数据库,下面简单翻译一下重点. No Sql的数据库可以分为如下4类: Key-Value数据库 数据 ...

  2. 用bootstrap结合php搭建MIS系统框架【转载】

    bootstrap目前比较火,抽空简单了解一下 官方介绍:Bootstrap, from Twitter基于HTML,CSS,JAVASCRIPT的简洁灵活的流行前端框架及交互组件集 http://t ...

  3. oracle中 sql%rowcount 使用方法

    sql%rowcount用于记录改动的条数,必须放在一个更新或者删除等改动类语句后面运行,select语句用于查询的话无法使用, 当你运行多条改动语句时,依照sql%rowcount 之前运行的最后一 ...

  4. android 20 Intnet类重要的成员变量

    Intnet类重要的成员变量: <intent-filter> <action android:name="android.intent.action.MAIN" ...

  5. Qt 学习之路 :文件

    文件操作是应用程序必不可少的部分.Qt 作为一个通用开发库,提供了跨平台的文件操作能力.从本章开始,我们来了解下 Qt 的文件以及输入输出的功能,也就是 I/O 系统. Qt 通过QIODevice提 ...

  6. [转] Android学习系列(29)--App调试的几个命令实践

    在Android的应用开发中,我们会用到各种代码调试:其实在Android的开发之后,我们可能会碰到一些随机的问题,如cpu过高,内存泄露等,我们无法简单的进行代码调试,我们需要一个系统日志等等,下面 ...

  7. VTMagic 的使用介绍

    VTMagic 有很多开发者曾尝试模仿写出类似网易.腾讯等应用的菜单分页组件,但遍观其设计,大多都比较粗糙,不利于后续维护和扩展.琢磨良久,最终决定开源这个耗时近两年打磨而成的框架,以便大家可以快速实 ...

  8. VS2015 Cordova Ionic移动开发(三)

    一.基础设置 1.修改App名称和程序起始页 打开config.xml配置文件显示如下,在[通用]选项卡中,将显示名称和起始页,修改为自己想要的名称即可. 如需直接在xml文件中修改:右击config ...

  9. C# gridview分頁導出excel

    #region 导出Excel方法 //导出到Excel按钮 protected void btnExport_Click(object sender, EventArgs e) { Export(& ...

  10. sublime text snippet代码片断

    $0 代表补全代码后放的位置   0 的权重是最低的 $1 最高 也等于${1:}  ${1: name}    1输入点的序号(1权重最高) name 自动补全的默认值      <conte ...