C#运行外部程序并后台等待...
2017-09-15 10:08
387 查看
备忘一下, 用ThreadPool开一个后台线程,在里面用Process对象执行指定外部应用,然后后台等待:
CardBean lvBean = getCardByPosition(e.ColumnIndex, e.RowIndex);
if (lvBean.status == CardBean.STATUS.stTesting) return;
ThreadPool.QueueUserWorkItem(new WaitCallback((object obj) =>
{
Process lvProc = new Process();
lvProc.StartInfo = new ProcessStartInfo(lvBean.exepath);
try
{
lvProc.Start();
lvBean.status = CardBean.STATUS.stTesting;
saveConfig();
dataGridView1.Invoke(new VoidDelegate(() => {
tslbl_label.Text = "正在运行\"" + lvBean.exepath + "\"...";
rebuildUI(mCards);
}));
lvProc.WaitForExit();
//check result
if (!File.Exists(lvBean.resultpath))
{
lvBean.status = CardBean.STATUS.stDoneNG;
}
else
{
string lvRet = getFileContent(lvBean.resultpath);
if (lvRet.ToUpper().Contains("PASS"))
{
lvBean.status = CardBean.STATUS.stDoneOK;
}
else if (lvRet.ToUpper().Contains("FAIL"))
{
lvBean.status = CardBean.STATUS.stDoneNG;
}
else{
MessageBox.Show(lvBean.resultpath + " 没有包含有效识别符.");
lvBean.status = CardBean.STATUS.stUntest;
}
}
saveConfig();
dataGridView1.Invoke(new VoidDelegate(() => {
tslbl_label.Text = "";
rebuildUI(mCards);
}));
}
catch (Exception ee)
{
dataGridView1.Invoke(new VoidDelegate(() => {
MessageBox.Show(ee.Message, Properties.Resources.LBL_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error);
}));
}
}));
CardBean lvBean = getCardByPosition(e.ColumnIndex, e.RowIndex);
if (lvBean.status == CardBean.STATUS.stTesting) return;
ThreadPool.QueueUserWorkItem(new WaitCallback((object obj) =>
{
Process lvProc = new Process();
lvProc.StartInfo = new ProcessStartInfo(lvBean.exepath);
try
{
lvProc.Start();
lvBean.status = CardBean.STATUS.stTesting;
saveConfig();
dataGridView1.Invoke(new VoidDelegate(() => {
tslbl_label.Text = "正在运行\"" + lvBean.exepath + "\"...";
rebuildUI(mCards);
}));
lvProc.WaitForExit();
//check result
if (!File.Exists(lvBean.resultpath))
{
lvBean.status = CardBean.STATUS.stDoneNG;
}
else
{
string lvRet = getFileContent(lvBean.resultpath);
if (lvRet.ToUpper().Contains("PASS"))
{
lvBean.status = CardBean.STATUS.stDoneOK;
}
else if (lvRet.ToUpper().Contains("FAIL"))
{
lvBean.status = CardBean.STATUS.stDoneNG;
}
else{
MessageBox.Show(lvBean.resultpath + " 没有包含有效识别符.");
lvBean.status = CardBean.STATUS.stUntest;
}
}
saveConfig();
dataGridView1.Invoke(new VoidDelegate(() => {
tslbl_label.Text = "";
rebuildUI(mCards);
}));
}
catch (Exception ee)
{
dataGridView1.Invoke(new VoidDelegate(() => {
MessageBox.Show(ee.Message, Properties.Resources.LBL_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error);
}));
}
}));
相关文章推荐
- c# 仿照计划任务(定时提示、定时运行程序、定时打开url(前台/后台))/每天/每周/每月/一次 多时间段
- 调用外部程序并等待其运行结束/实例为调用QQ截图
- delphi 调用外部exe ,并等待调用程序运行结束
- C#之使用NotifyIcon实现任务栏托盘菜单,图标闪烁效果及气泡提示 很多程序是只需要后台运行的,甚至不需要自己的应用界面。NotifyIcon提供了程序在任务栏的显示功能 程序下载链接如下
- Delphi 调用外部程序并等待其运行结束
- C#运行外部程序
- c# winform将打开的程序置于后台运行的代码
- c# 仿照计划任务(定时提示、定时运行程序、定时打开url(前台/后台))/每天/每周/每月/一次 多时间段
- c# 程序后台运行
- C#软件监控外部程序运行状态
- C#软件监控外部程序运行状态
- MASM32编程用两种方法运行外部程序并等待其结束
- [delphi]调用外部程序并等待其运行结束
- C#实现软件监控外部程序运行状态的方法
- c# 使用Process调用外部程序时等待该进程结束后再执行住进程
- C#如何运行外部程序(打开可执行程序):ShellExcute和Process
- 如何让Delphi调用外部程序并等待其运行信息(如结束)
- Delphi 调用外部程序并等待其运行结束
- 今天学习C#,在dos下用csc命令编译,提示“csc不是内部或外部命令,也不是可运行的程序... ”
- 如何让Delphi调用外部程序并等待其运行信息(如结束)