Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to execute multiples .BAT files in C#
    primarykey
    data
    text
    <p>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<br> attached the piece of code i used hop you'll find a sample windows form app.</p> <p>Thanks in advance.</p> <p>Francesco</p> <pre><code>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"; } } } </code></pre> <p>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?</p> <p>so the new version of Form1's code is:</p> <pre><code>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&lt;String&gt; 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&lt;String&gt;(); 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 &amp;&amp; batchFiles.Count &gt; 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 &amp;&amp; !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"; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload