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

Related Questions

I'm writing a custom form UI and currently building a select box replacement. The
I am writing a homescreen replacement App. I'd like to allow users the same
I am writing a custom replacement for the Textbox/Label controls. It needs to provide
I am writing a drop-in replacement for a legacy application in Java. One of
Writing some test scripts in IronPython, I want to verify whether a window is
Im new to writing macros in Xcode and am having a problem writing my
I'm writing a translator, not as any serious project, just for fun and to
I'm writing an iPod app replacement, and in my now playing view I want
I'm currently writing a Python sandbox using sandboxed PyPy. Basically, the sandbox works by
I don't have a Dock, but I'm writing an Dock replacement app. Can I

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.