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

  • Home
  • SEARCH
  • 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 264597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T22:41:56+00:00 2026-05-11T22:41:56+00:00

I am writing a replacement Windows shell in C#, and the feature I am

  • 0

I am writing a replacement Windows shell in C#, and the feature I am currently implementing is having all IO inside the same shell window – ie, not opening cmd.exe in another window.

Right now, I have a partial solution to output. This is my code (where p is the running Process):

while ( !p.HasExited ) {
    /* ... - irrelevant */
    if ( redirect ) {
        try {
            p.BeginOutputReadLine();
        } catch { }
    }
}

The process is set up with all the correct properties, such asUseShellExecute = False, RedirectStandard{Input, Output, Error} = True, and the event handlers are set up correctly, but it’s inconsistent.

I have tried taking out the try/catch (which I know is extremely bad practice), and using a busy bool, which is set to false when the handler runs, but for some reason, I still get an InvalidOperationException on p.BeginOutputReadLine() – stating that there is already an async operation running.

Any help would be appreciated, even if it requires a completely different solution to the one above, rather than just fixing it.

Thanks.

EDIT: Here is the code which starts the process:

if (redirect)
{
    p.StartInfo.RedirectStandardOutput = true;
    p.StartInfo.RedirectStandardError = true;
    p.StartInfo.RedirectStandardInput = true;
    p.StartInfo.UseShellExecute = false;
    p.OutputDataReceived += new DataReceivedEventHandler(redirectHandler_StdOut);
    p.ErrorDataReceived += new DataReceivedEventHandler(redirectHandler_StdErr);
}
p.Start();

Also, I’ve realised that I’m not explaining what I mean by inconsistent. As they say, a picture is worth 2^3 words:

screenshot

  • 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-11T22:41:56+00:00Added an answer on May 11, 2026 at 10:41 pm

    Methods prefixed with Begin typically run asynchronously. Is this what you want? Because it appears that the loop runs as fast as it can, calling more and more BeginOutputReadLine (because the call returns immediately, before it’s done and the loop comes around for another iteration).

    You could call ReadLine on the StandardOutput stream for a synchronous solution. Or:

        while ( !p.HasExited ) {
            /* ... - irrelevant */
            if ( redirect && !busy ) {
                try {
                    busy = true;
                    p.BeginOutputReadLine();
                } catch { }
            }
        }
       ////In the method that gets called upon completion of BeginOutputReadLine
       busy = false;
    

    Keep this in mind though (from MSDN):

    When asynchronous read operations
    start, the event handler is called
    each time the associated Process
    writes a line of text to its
    StandardOutput stream.

    You can cancel an asynchronous read
    operation by calling CancelOutputRead.
    The read operation can be canceled by
    the caller or by the event handler.
    After canceling, you can call
    BeginOutputReadLine again to resume
    asynchronous read operations.

    That leads me to believe you should only call this method once and it will keep notifying you through callback when a line gets written.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 146k
  • Answers 146k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer PHP 5.6+ introduced const arrays - see Andrea Faulds' answer.… May 12, 2026 at 9:02 am
  • Editorial Team
    Editorial Team added an answer Entries.hasOwnProperty("j") does not work because you're sending it "j" as… May 12, 2026 at 9:02 am
  • Editorial Team
    Editorial Team added an answer You write a FilterInputStream which will throw an exception if… May 12, 2026 at 9:02 am

Related Questions

I am writing a drop-in replacement for a legacy application in Java. One of
I am writing a minimal replacement for mod_python's publisher.py The basic premise is that
I am writing a simple image replacement script on a site I am working
A system I am writing uses Markdown to modify links, but I also want

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.