How to use System.Diagnostics.Process correctly
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:
- Not capturing both output streams (error & output)
- Not redirecting input can cause applications to hang
- Not closing redirected input can cause applications to hang
- Not calling BeginOutputReadLine/BeginErrorReadLine when using events
- Using OutputDataReceived/ErrorDataReceived without waiting for null
- Not checking for null in OutputDataReceived/ErrorDataReceived handlers
- Forgetting to set EnableRaisingEvents = true; when using the Exited event
- Forgetting ErrorDialog, CreateNoWindow, or UseShellExecute settings
- Incorrect handling of StandardOutput or StandardError stream readers
So with this said, here are some basic guidelines:
- Use the OutputDataReceived/ErrorDataRecieved events NOT the StandardOutput or StandardError. This will save you a lot of headache and needless thread management.
- Always capture all output AND input, if you don’t plan to provide input, close the stream immediately.
- 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:
- ProcessRunner library class source code, online help, or see the usage example.
- For a clean stand-alone example see “Using Process.Start to capture console output“
- Escaping arguments: “How to correctly escape command line arguments in c#“
- Finding an Executable: “How to search the environment’s path for an exe or dll“
How to use System.Diagnostics.Process correctly的更多相关文章
- System.Diagnostics.Process.Start的妙用
我们经常会遇到在Winform或是WPF中点击链接或按钮打开某个指定的网址, 或者是需要打开电脑中某个指定的硬盘分区及文件夹, 甚至是"控制面板"相关的东西, 那么如何做呢? 答案 ...
- System.Diagnostics.Process 启动进程资源或调用外部的命令的使用
经常看到一些程序在保存为一个txt,或者excel的文件的时候,保存完毕立即打开, 启动程序或打开文件的代码 System.Diagnostics.Process.Start(System.IO.Pa ...
- System.Diagnostics.Process.Star的用法
System.Diagnostics.Process.Start(); 能做什么呢?它主要有以下几个功能: 1.打开某个链接网址(弹窗). 2.定位打开某个文件目录. 3.打开系统特殊文件夹,如“控制 ...
- WPF中System.Diagnostics.Process.Start的妙用
我们经常会遇到在Winform或是WPF中点击链接或按钮打开某个指定的网址, 或者是需要打开电脑中某个指定的硬盘分区及文件夹, 甚至是"控制面板"相关的东西, 那么如何做呢? 答案 ...
- C# 之 System.Diagnostics.Process.Start的妙用
经常会遇到在Winform或是WPF中点击链接或按钮打开某个指定的网址, 或者是需要打开电脑中某个指定的硬盘分区及文件夹, 甚至是"控制面板"相关的东西, 如何做呢? 方法:使用S ...
- C#——System.Diagnostics.Process.Start的妙用
我们经常会遇到在Winform或是WPF中点击链接或按钮打开某个指定的网址, 或者是需要打开电脑中某个指定的硬盘分区及文件夹, 甚至是"控制面板"相关的东西, 那么如何做呢? 答案 ...
- System.Diagnostics.Process 执行.EXE
分类: C#+WINFORM 2009-04-05 21:09 459人阅读 评论(0) 收藏 举报 我们经常会遇到在Winform或是WPF中点击链接或按钮打开某个指定的网址, 或者是需要打 ...
- System.Diagnostics.Process 测试案例
1.System.Diagnostics.Process 执行exe文件 创建项目,编译成功后,然后把要运行的exe文件拷贝到该项目的运行工作目录下即可,代码如下: using System; usi ...
- (转)C# System.Diagnostics.Process.Start使用
经常会遇到在Winform或是WPF中点击链接或按钮打开某个指定的网址, 或者是需要打开电脑中某个指定的硬盘分区及文件夹, 甚至是"控制面板"相关的东西, 如何做呢? 方法:使用 ...
随机推荐
- HDU--5280(dp或枚举)
官方题解: 这个题有非常多O(n2)的算法.这里说一种:枚举每个区间,在枚举区间的同一时候维护区间内的最小值和区间和,将最小值与P的大小进行比較,贪心地取最大值就可以.注意若枚举到的区间是整个数组,则 ...
- CFS: 虚拟运行时间
http://edsionte.com/techblog/archives/4331 nice和prio的关系如下: #define NICE_TO_PRIO(nice) (MAX_RT_PRIO+n ...
- [转] Python特殊语法:filter、map、reduce、lambda
Python内置了一些非常有趣但非常有用的函数,充分体现了Python的语言魅力! filter(function, sequence):对sequence中的item依次执行function(ite ...
- java解析xml文件四种方式
1.介绍 1)DOM(JAXP Crimson解析器) DOM是用与平台和语言无关的方式表示XML文档的官方W3C标准.DOM是以层次结构组织的节点或信息片断的集合.这个层次结构允许开发人员在树中寻找 ...
- Java设计模式03:常用设计模式之单例模式(创建型模式)
1. Java之单例模式(Singleton Pattern ) 单例模式是一种常见的设计模式,单例模式分三种:懒汉式单例.饿汉式单例.登记式单例三种. 单例模式有一下特点: 1.单例类只能有一个实 ...
- date和long的相互转换
import java.text.SimpleDateFormat; import java.util.Date; public class T { public static void main(S ...
- 【css面试题】三个DIV要求水平对齐,左右两个DIV宽度固定为100px,中间那个DIV充满剩余的宽度(至少2种方法)
这是我在一家公司面试时遇到的问题,当时没有答上来!! 所以看到的小伙伴一定要注意了!! 变化浏览器宽度可看到效果: 左 右 中 然后我们来看看代码: 第一种方法:(浮动) <style type ...
- DNS服务器的原理
当用户去访问一个网站(百度)的时候,首先去请求dns服务器,根据对应的域名返回所在ip,然后再使用ip去访问自己所在服务器空间.简单的说,DNS服务器就像114客服,dns服务器是树状结构的,dns服 ...
- 服务器证书安装配置指南(IIS7.5) 分类: ASP.NET 2014-11-05 12:39 105人阅读 评论(0) 收藏
1.启动IIS管理器,点击开始菜单->所有程序->管理工具->Internet信息服务(IIS)管理器: 2.选择"服务器证书": 3.在右边窗口,选择" ...
- 正则表达式匹配(node.js)
参考文档如下: http://blog.csdn.net/huiguixian/article/details/6131048 http://www.91xueke.com/2013/04/05/30 ...