Note that there are some explanatory texts on larger screens.

plurals
  1. POWaiting for commands to be complete
    primarykey
    data
    text
    <p>I am working with a winform that runs a cmd in the background, redirecting input and output asynchronously.</p> <p>Currently, the winform iterating through an array of commands, writing each to the cmd via the StreamWriter the StandardInput is redirected to. How can I force the loop to wait until the present command is complete in the cmd before writing the next line in?</p> <p><strong>EDIT:</strong> I took out all of my actual project code, and replaced it with this, a stripped down version of what I'm trying to do, only including components of my project relevant to my question.</p> <pre><code>public partial class Form1 : Form { public delegate void WriteToConsoleMethod(string text); Process _process; string[] _commands = { "echo hello world", "echo my name is T.K.", "echo Here is a list of commands" }; public Form1() { InitializeComponent(); ProcessStartInfo processStartInfo = new ProcessStartInfo("cmd") { RedirectStandardError = true, RedirectStandardInput = true, RedirectStandardOutput = true, UseShellExecute = false, CreateNoWindow = true }; _process = Process.Start(processStartInfo); _process.OutputDataReceived += new DataReceivedEventHandler(new DataReceivedEventHandler(DataReceived_EventHandler)); _process.ErrorDataReceived += new DataReceivedEventHandler(new DataReceivedEventHandler(DataReceived_EventHandler)); _process.BeginErrorReadLine(); _process.BeginOutputReadLine(); } private void DataReceived_EventHandler(object sender, DataReceivedEventArgs e) { IAsyncResult result = this.BeginInvoke(new WriteToConsoleMethod(writeToConsole), new object[] { e.Data + Environment.NewLine }); this.EndInvoke(result); } private void writeToConsole(string output) { txtbxConsole.AppendText(output); } private void btnBegin_Click(object sender, EventArgs e) { foreach (string command in _commands) { _process.StandardInput.WriteLine(command); // I want a way to pause here until the cmd has finished processing the command. } } } </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