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 8999097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:08:06+00:00 2026-06-16T00:08:06+00:00

I have application that launches sub process and processes its stdout asynchronously. The problem

  • 0

I have application that launches sub process and processes its stdout asynchronously. The problem is that the async operation takes some time and I want method responsible for process execution to end after all async IO operations are done.

I have code like this:

using System.Diagnostics;

Process process = new Process();
// ...
process.OutputDataReceived += new DataReceivedEventHandler(this.OnRecvStdOut);
process.ErrorDataReceived += new DataReceivedEventHandler(this.OnRecvStdErr);
// ...
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
// ...
process.WaitForExit();

Now I’m looking for a way how to tell the program to wait until all IO (OnRecvStdOut) operations are done.

I though about using one of System.Threading classes, but I’m not sure which class is the best for this and how to do this after all, the best way would probably be:

public void OnRecvStdOut(...)
{
    something.Increase();
    // the stuff that takes so long
    something.DecreaseAndSignal();
}

And in main function:

something.WaitUntilZero();

Note: I’d like to allow both StdErr and StdOut to be processed in parallel. Something can’t rely that Wait will be called as much as Signal, because Increase() and DecreaseAndSignal() pairs will be called many times before Wait occurs.

The second thing that came to mind was something that would be able to signal many times (without requirement to process signals) and use loop in main function like:

while( ioOperations > 0){
    something.WaitForSignal(500);
}

Edit: Current working solution:

I came up with this what seems to be working:

using System.Threading;

protected int IOCount = 0;
protected AutoResetEvent _IOSyncEvent = new AutoResetEvent(false);

public void OnRecvStdOut(...)
{
    Interlocked.Increase( ref IOCount);
    // the stuff that takes so long
    Interlocked.Decrease( ref IOCount);
    IOSyncEvent.Set();
}

// After process.WaitForExit()
while( IOCount > 0){
    // 250 for a case that signal occurs after condition and before wait
    IOSyncEvent.WaitOne(250); 
}

But I’d appreciate any notes how to do this “good practice way” or what are possible risks of implementation I though of.

  • 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-06-16T00:08:06+00:00Added an answer on June 16, 2026 at 12:08 am

    Use a CountdownEvent set to 2. Each data received callback, check the x.EndOfStream flag (where x is standardoutput or standarderror on the process object). If EOF, then signal the countdownevent. It will work because both the standard and error output streams will eventually signal the event. Once both signal it, the wait on it will return. It is also a very efficient way to do it.

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

Sidebar

Related Questions

I have a small application that launches a login activity as a sub activity
here's my problem: I have an application that launches a simulated server locally. The
I have created an application that launches a couple of child processes using fork
I have a Java application that launches an external process (Internet Explorer) using ProcessBuilder.
I have a function that launches programs asynchronously: (defun app (app-name) (interactive sRun application:
I have an android application that launches a child Activity under certain situations. Both
I have a MFC application that launches a IWebBrowser2 window. On users computers where
I have a simple Qt application that launches a window with a QWebView. I
I have an application that uses a tab bar and whenever it launches it
I have an application that fires 2 threads, the 1st launches another class to

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.