首页 技术 正文
技术 2022年11月16日
0 收藏 386 点赞 2,723 浏览 1463 个字
public int CallPhoneExe(string arg) //arg为进程的命令行参数
{
WaitHandle[] waits =new WaitHandle[2]; //定义两个WaitHandle值,用以控制进程的执行过程
waits[0] = HSTOP; //AutoResetEvent HSTOP = new AutoResetEvent(false);
waits[1] = GlobalStop;//AutoResetEvent GlobalStop = new AutoResetEvent(false);
int iReturn=0;
Process p = new Process();//新建一个进程
p.StartInfo.Arguments = arg; //进程的命令行参数
p.StartInfo.FileName = filepath;//进程启动路径 p.StartInfo.CreateNoWindow = true;//不显示新进程的窗口
p.StartInfo.RedirectStandardOutput = true;//输出重定向
p.StartInfo.RedirectStandardError = true; //Redirect the error ouput!
p.StartInfo.UseShellExecute = false;
p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; p.EnableRaisingEvents = true;
p.Exited += new EventHandler(p_Exited); //进程自然结束后启动p—Exited事件
p.OutputDataReceived += new DataReceivedEventHandler(ChangeOutput);//进程有输出时,启动ChangeOutPut函数 p.Start();//进程启动
p.BeginOutputReadLine(); int hstop = WaitHandle.WaitAny(waits);//启动线程暂停,知道WaitHandle中传来有效信号
switch (hstop)//判断信号是又哪个
{
case 0: //进程自然结束
if (p.WaitForExit(2000))
iReturn = p.ExitCode; //获取进程的返回值
else
{
CloseProcess();
iReturn = -2;
}
break;
case 1: //进程被迫结束
p.Kill();//杀掉进程
if (!p.HasExited)
{
p.Kill();
}
iReturn = -3;
break;
}
HSTOP.Reset(); //HSTOP复位,这个变量指示进程自然结束,每次结束后都得自然复位
p.Close(); //创建的p关闭
return iReturn;
} private void p_Exited(object sender, EventArgs e)
{
HSTOP.Set();
} //输出重定向函数
private void ChangeOutput(object sendingProcess, DataReceivedEventArgs outLine)
{
if (!String.IsNullOrEmpty(outLine.Data)) //字符串不为空时
MainForm.FireWriteText(outLine.Data,false);//将进程的输出信息转移
}

上述代码基本囊括了对进程Process的操作。在C#工具箱中包括进程这一个组件。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,495
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,295