Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4014266
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:28:33+00:00 2026-05-20T09:28:33+00:00

I am working with a winform that runs a cmd in the background, redirecting

  • 0

I am working with a winform that runs a cmd in the background, redirecting input and output asynchronously.

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?

EDIT: 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.

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.
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T09:28:34+00:00Added an answer on May 20, 2026 at 9:28 am

    I don’t think there is anything built-in that will support that. However you could send your own special command and then wait until you see this in the output for example ,
    something like :

        const string Separator= "---Command Completed--\xE3\xE2\xE1\xE0\xE3";   
        // Has to be something that won't occur in normal output.  
    
        volatile bool finished = false;
    
        private void button1_Click(object sender, EventArgs e)
        {         
            foreach (string command in _commands)
                Run(command);
        }
    
        private void writeToConsole(string output)
        {
            if (output.IndexOf(Separator) >= 0)
                finished = true;
            else
                richTextBox1.AppendText(output);
        }
    
    
        private void Run(string command)
        {
            finished = false;
            _process.StandardInput.WriteLine(command);
            _process.StandardInput.WriteLine("@echo " + Seperator);
            while (!finished)
            {
                Application.DoEvents();
                System.Threading.Thread.Sleep(100);
            }
        }
    
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            finished = true;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Good morning, I am working on a C# winform application that is using validation
I'm working on a WinForm .Net application with the basic UI that includes toolbar
I'm working on a winform app and am interating through a checkboxlist to see
Background: I will be working on tools that will be dependent on a rapidly
I am working on a WinForm application, that allows working to work with projects
I am currently working on a project that hosts WinForms designer. I am using
I'm working on a .NET WinForms app that needs to print a FEDEX shipping
I am working on a VB.NET WinForms app that was upgraded by Visual Studio
I have a DataGridView control in a winforms app that I'm working on. The
I have been working with Visual Studio (WinForm and ASP.NET applications using mostly C#)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.