如何执行在C#.bat文件倍数 [英] How to execute multiples .BAT files in C#
问题描述
我需要使用C#Windows窗体应用程序执行倍数批处理文件。就在包含一个或多个线像蝙蝠的情况下:开始文件名.exe我的计划等待,直到文件名.exe将被终止,显然这不是我需要什么,我已经结果
附件code,我用跳,你会发现一个示例Windows窗体应用程序。的
先谢谢了。
弗朗西斯
使用系统;
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data这;
使用System.Diagnostics程序;
使用System.Drawing中;
使用System.IO;
使用System.Linq的;
使用System.Text;
使用System.Threading.Tasks;
使用System.Windows.Forms的;命名空间示例
{
公共部分Form1类:表格
{ / *
*批处理文件
*
C:\\ batch1.bat
回声startExecution
启动/ B的calc.exe
回声endExecution
出口
*
*
C:\\ batch2.bat
回声startExecution2
启动/ B NOTEPAD.EXE
回声endExecution2
出口 * / 公共Form1中()
{
//只是样品是不安全
Control.CheckForIllegalCrossThreadCalls = FALSE;
的InitializeComponent();
this.button1.Click + =的button1_Click;
}
私人无效的button1_Click(对象发件人,EventArgs的发送)
{
this.richTextBox1.Text =初始化\\ r \\ n;
BatchExecution是=新BatchExecution(C:\\\\ batch1.bat);
be.endOccurs + = be_endOccurs;
be.DoWork();
是=新BatchExecution(C:\\\\ batch2.bat);
be.endOccurs + = be_endOccurs;
be.DoWork();
} 私人无效be_endOccurs(BatchExecution发送方)
{
this.richTextBox1.AppendText(sender.output);
发送= NULL;
}
} 公共类BatchExecution
{ 私人字符串批量{搞定;组; }
公共流程过程{搞定;私人集; } 公共委托无效workHasEndedHandler(BatchExecution发送者);
公共事件workHasEndedHandler endOccurs; 私人布尔_hasEnded = FALSE;
公共布尔hasEnded
{
得到
{
返回_hasEnded;
}
组
{
_hasEnded =价值;
如果(_hasEnded)
{
endOccurs(本);
}
}
} 公共字符串输出{搞定;组; } 公共BatchExecution(字符串batFile)
{
批量= batFile;
} 私人无效workCompleted()
{
如果(处理!= NULL)
{
process.Close();
process.Dispose();
GC.Sup pressFinalize(过程);
流程= NULL;
} 输出+ =批量结束\\ r \\ n;
hasEnded = TRUE;
} 公共无效的DoWork()
{
输出=批量输出:\\ r \\ n;
流程=新工艺();
process.StartInfo.CreateNoWindow = TRUE;
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.StartInfo.FileName =cmd.exe的;
process.StartInfo.Arguments =/ C \\+批次+\\;
process.StartInfo.UseShellExecute = FALSE;
process.StartInfo.RedirectStandardOutput =真;
process.StartInfo.RedirectStandardError = TRUE;
process.ErrorDataReceived + = process_ErrorDataReceived;
process.OutputDataReceived + = process_OutputDataReceived;
的Process.Start();
process.BeginOutputReadLine();
process.WaitForExit();
workCompleted();
} 私人无效process_ErrorDataReceived(对象发件人,DataReceivedEventArgs E)
{
输出+ =+ e.Data +\\ r \\ n;
} 私人无效process_OutputDataReceived(对象发件人,DataReceivedEventArgs E)
{
输出+ =+ e.Data +\\ r \\ n;
}
}}
从肯尼斯建议,开始我删除process.WaitForExit()。现在用一个BackgroundWorker如果批处理执行完毕我可以检查。这似乎解决了,但我不很喜欢。任何人有更好的主意?
所以Form1的code的新版本是:
使用系统;
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data这;
使用System.Diagnostics程序;
使用System.Drawing中;
使用System.IO;
使用System.Linq的;
使用System.Text;
使用System.Threading.Tasks;
使用System.Windows.Forms的;命名空间示例
{
公共部分Form1类:表格
{ / *
*批处理文件
*
C:\\ batch1.bat
回声startExecution
启动/ B的calc.exe
平1.1.1.1 -n 1 -w 10000
回声endExecution
出口 *
*
C:\\ batch2.bat
回声startExecution2
启动/ B NOTEPAD.EXE
回声endExecution2
出口 * / 私人列表<串GT;批处理文件中获取{;组; }
私人的Int32 batchIndex {搞定;组; }
私人BatchExecution是{搞定;组; } 公共Form1中()
{
//只是样品是不安全
Control.CheckForIllegalCrossThreadCalls = FALSE;
的InitializeComponent();
this.button1.Click + =的button1_Click;
} 私人无效的button1_Click(对象发件人,EventArgs的发送)
{
batchIndex = 0;
批处理文件中=新的List<串GT;();
batchFiles.Add(C:\\\\ batch1.bat);
batchFiles.Add(C:\\\\ batch2.bat);
this.richTextBox1.Text =初始化\\ r \\ n; 是=新BatchExecution(批处理文件中[batchIndex]);
be.endOccurs + = be_endOccurs;
be.DoWork();
} 私人无效be_endOccurs(BatchExecution发送方)
{
this.richTextBox1.AppendText(sender.output);
如果(sender.process!= NULL)
{
sender.process.Close();
sender.process.Dispose();
GC.Sup pressFinalize(sender.process);
sender.process = NULL;
}
发送= NULL;
batchIndex ++; 如果(批处理文件中= NULL&放大器;!&安培; batchFiles.Count> batchIndex)
{
是=新BatchExecution(批处理文件中[batchIndex]);
be.endOccurs + = be_endOccurs;
be.DoWork();
} }
} 公共类BatchExecution
{
私人字符串批量{搞定;组; }
公共流程过程{搞定;组; } 私人BackgroundWorker的asyncVerifier {搞定;组; } 公共委托无效workHasEndedHandler(BatchExecution发送者);
公共事件workHasEndedHandler endOccurs; 私人布尔_hasEnded = FALSE;
公共布尔hasEnded
{
得到
{
返回_hasEnded;
}
私订
{
_hasEnded =价值;
如果(_hasEnded)
{
如果(asyncVerifier!= NULL)
{
asyncVerifier.Dispose();
GC.Sup pressFinalize(asyncVerifier);
asyncVerifier = NULL;
输出+ =批量结束\\ r \\ n;
}
endOccurs(本);
}
}
} 公共字符串输出{搞定;组; } 公共BatchExecution(字符串batFile)
{
批量= batFile;
} 公共无效的DoWork()
{
输出=批量输出:\\ r \\ n;
asyncVerifier =新的BackgroundWorker();
asyncVerifier.DoWork + = asyncVerifier_DoWork;
流程=新工艺();
process.StartInfo.CreateNoWindow = TRUE;
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.StartInfo.FileName =cmd.exe的;
process.StartInfo.Arguments =/ C \\+批次+\\;
process.StartInfo.UseShellExecute = FALSE;
process.StartInfo.RedirectStandardOutput =真;
process.StartInfo.RedirectStandardError = TRUE;
process.ErrorDataReceived + = process_DataReceived;
process.OutputDataReceived + = process_DataReceived;
的Process.Start();
asyncVerifier.RunWorkerAsync();
process.BeginOutputReadLine();
} 私人无效asyncVerifier_DoWork(对象发件人,DoWorkEventArgs E)
{
布尔performCheck =真; 而(performCheck)
{
如果(过程= NULL&放大器;!&安培;!process.HasExited)
{
System.Threading.Thread.Sleep(500);
}
其他
{
performCheck = FALSE;
}
}
hasEnded = TRUE;
} 私人无效process_DataReceived(对象发件人,DataReceivedEventArgs E)
{
输出+ =+ e.Data +\\ r \\ n;
}
}}
如果您删除调用 process.WaitForExit();
程序将继续,而不是等到退出进程。
显然,你为 WorkCompleted
通话不仅仅只是意味着所有批次开始了。
I need to execute multiples batch files using "c# windows form application". Just in case of bat containing one or more line like: "start filename.exe" my program waits until "filename.exe" will be terminated, and obviously this isn't what i need i have
attached the piece of code i used hop you'll find a sample windows form app.
Thanks in advance.
Francesco
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Sample
{
public partial class Form1 : Form
{
/*
* Batch files
*
c:\batch1.bat
echo startExecution
start /b calc.exe
echo endExecution
exit
*
*
c:\batch2.bat
echo startExecution2
start /b notepad.exe
echo endExecution2
exit
*/
public Form1()
{
//Just for sample is unsafe
Control.CheckForIllegalCrossThreadCalls = false;
InitializeComponent();
this.button1.Click += button1_Click;
}
private void button1_Click(object sender, EventArgs e)
{
this.richTextBox1.Text = "Initialized\r\n";
BatchExecution be = new BatchExecution("c:\\batch1.bat");
be.endOccurs += be_endOccurs;
be.DoWork();
be = new BatchExecution("c:\\batch2.bat");
be.endOccurs += be_endOccurs;
be.DoWork();
}
private void be_endOccurs(BatchExecution sender)
{
this.richTextBox1.AppendText(sender.output);
sender = null;
}
}
public class BatchExecution
{
private String batch { get; set; }
public Process process { get; private set; }
public delegate void workHasEndedHandler(BatchExecution sender);
public event workHasEndedHandler endOccurs;
private Boolean _hasEnded = false;
public Boolean hasEnded
{
get
{
return _hasEnded;
}
set
{
_hasEnded = value;
if (_hasEnded)
{
endOccurs(this);
}
}
}
public String output { get; set; }
public BatchExecution(String batFile)
{
batch = batFile;
}
private void workCompleted()
{
if (process != null)
{
process.Close();
process.Dispose();
GC.SuppressFinalize(process);
process = null;
}
output += "Batch ended\r\n";
hasEnded = true;
}
public void DoWork()
{
output = "Batch output:\r\n";
process = new Process();
process.StartInfo.CreateNoWindow = true;
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = " /c \"" + batch + "\"";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.ErrorDataReceived += process_ErrorDataReceived;
process.OutputDataReceived += process_OutputDataReceived;
process.Start();
process.BeginOutputReadLine();
process.WaitForExit();
workCompleted();
}
private void process_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
output += "" + e.Data + "\r\n";
}
private void process_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
output += "" + e.Data + "\r\n";
}
}
}
Starting from Kenneth suggestion i removed "process.WaitForExit()". Now using a BackgroundWorker i can check if batch execution is completed. It seems solved but i don't like it very much. Anybody has better idea?
so the new version of Form1's code is:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Sample
{
public partial class Form1 : Form
{
/*
* Batch files
*
c:\batch1.bat
echo startExecution
start /b calc.exe
ping 1.1.1.1 -n 1 -w 10000
echo endExecution
exit
*
*
c:\batch2.bat
echo startExecution2
start /b notepad.exe
echo endExecution2
exit
*/
private List<String> batchFiles { get; set; }
private Int32 batchIndex { get; set; }
private BatchExecution be { get; set; }
public Form1()
{
//Just for sample is unsafe
Control.CheckForIllegalCrossThreadCalls = false;
InitializeComponent();
this.button1.Click += button1_Click;
}
private void button1_Click(object sender, EventArgs e)
{
batchIndex = 0;
batchFiles = new List<String>();
batchFiles.Add("c:\\batch1.bat");
batchFiles.Add("c:\\batch2.bat");
this.richTextBox1.Text = "Initialized\r\n";
be = new BatchExecution(batchFiles[batchIndex]);
be.endOccurs += be_endOccurs;
be.DoWork();
}
private void be_endOccurs(BatchExecution sender)
{
this.richTextBox1.AppendText(sender.output);
if (sender.process != null)
{
sender.process.Close();
sender.process.Dispose();
GC.SuppressFinalize(sender.process);
sender.process = null;
}
sender = null;
batchIndex++;
if (batchFiles != null && batchFiles.Count > batchIndex)
{
be = new BatchExecution(batchFiles[batchIndex]);
be.endOccurs += be_endOccurs;
be.DoWork();
}
}
}
public class BatchExecution
{
private String batch { get; set; }
public Process process { get; set; }
private BackgroundWorker asyncVerifier { get; set; }
public delegate void workHasEndedHandler(BatchExecution sender);
public event workHasEndedHandler endOccurs;
private Boolean _hasEnded = false;
public Boolean hasEnded
{
get
{
return _hasEnded;
}
private set
{
_hasEnded = value;
if (_hasEnded)
{
if (asyncVerifier != null)
{
asyncVerifier.Dispose();
GC.SuppressFinalize(asyncVerifier);
asyncVerifier = null;
output += "Batch ended\r\n";
}
endOccurs(this);
}
}
}
public String output { get; set; }
public BatchExecution(String batFile)
{
batch = batFile;
}
public void DoWork()
{
output = "Batch output:\r\n";
asyncVerifier = new BackgroundWorker();
asyncVerifier.DoWork += asyncVerifier_DoWork;
process = new Process();
process.StartInfo.CreateNoWindow = true;
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = " /c \"" + batch + "\"";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.ErrorDataReceived += process_DataReceived;
process.OutputDataReceived += process_DataReceived;
process.Start();
asyncVerifier.RunWorkerAsync();
process.BeginOutputReadLine();
}
private void asyncVerifier_DoWork(object sender, DoWorkEventArgs e)
{
Boolean performCheck = true;
while (performCheck)
{
if (process != null && !process.HasExited)
{
System.Threading.Thread.Sleep(500);
}
else
{
performCheck = false;
}
}
hasEnded = true;
}
private void process_DataReceived(object sender, DataReceivedEventArgs e)
{
output += "" + e.Data + "\r\n";
}
}
}
If you remove the call to process.WaitForExit();
the program will continue and not wait until the process exits.
Obviously, your call to WorkCompleted
than just merely means that all batches were started.
这篇关于如何执行在C#.bat文件倍数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!