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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T08:59:57+00:00 2026-06-03T08:59:57+00:00

I’m trying to use C# to control a command line application background, which can

  • 0

I’m trying to use C# to control a command line application background, which can be downloaded here: http://www.ti.com/litv/zip/spmc015b

This is an app for motor voltage control, when I enter the app, like “b.exe -c 1”, the console seems a kind of blocking model. Every command in this app begins with “#” symbol. See pics here:

http://i46.tinypic.com/zx5edv.jpg

What I’m trying to do is, use StandardInput.WriteLine(“stat vout”); to measure the voltage. This will send a “stat vout” command to the console background and ideally return a voltage value. In this pic, it return some help hints. Duing all this time, it still in the blocking mode.

I want to get the return message with StandardOutput.ReadLine(); but failed. If ReadToEnd() then my program is freezed because this app never return to standard console, which is blocking.

When I tried BeginOutputReadLine(); OutputDataReceived event can truly obtain the message return from the console, like in the pics of “stat [vout|vbus|fault”. But it limited in my single thread program.

My current situation is that, I use System.Timers.Timers in WinForms and every second will send a “stat vout2” command to read the voltage, and hopefully get the return value.

However, the System.Timers.Timers is asynchronous, so when I called BeginOutputReadLine() in this Timers thread, it prompted “An async read operation has already been started on the stream.” In the meantime, as I’ve demonstrated above, I cannot use synchronous methods like ReadLine() to get the value.

So what should I do now? I truly need to run this command line app in multi-threading mode.

Thanks so much and wish everybody has a nice weekend.

–UPDATE on Apr 28 19:18 CST
Here is the relevant source code:

One of the WinFroms button will Start() the SystemClock Class, then Timing.Measuring() is executed every second. The TimingController Class will call GetVoltage() and GetCurrent() at the same time during one second according to the SystemClock, to measure the voltage and current.

In the Measuring Class, StandardInput.WriteLine(“stat vout2”); to get the voltage from the console app, and StandardInput.WriteLine(“stat cur”); to get the current. Both of them use BeginOutputReadLine() to get result since StandardOutput didn’t work.

I use a isOutputObtained flag to indicating if data returned. Every time the reading is finished, I did call CancelOutputRead(); to cancel asynchronous read.

But it still give me the error exception of “An asynchronous read operation is already in progress on the StandardOutput stream”

public class SystemClock
{
    TimingController Timing = new TimingController();
    private Timer TimerSystemClock;

    public SystemClock()
    {
        TimerSystemClock = new Timer();
        TimerSystemClock.Interval = 1000;
        TimerSystemClock.AutoReset = true;
        TimerSystemClock.Elapsed += new ElapsedEventHandler(TimerSystemClock_Elapsed);
        TimerSystemClock.Enabled = false;
        Timing.ClockInstance = this;
    }

    public void Start()
    {
        TimerSystemClock.Enabled = true;
    }

    void TimerSystemClock_Elapsed(object sender, ElapsedEventArgs e)
    {
        Timing.Measuring();
    }
}

public class TimingController
{
    // Get singleton of Measurement Class
    Measurement Measure = Measurement.GetInstance();       

    public SystemClock ClockInstance
    {
        get { return Clock; }
        set { Clock = value; }
    }

    private void Measuring()
    {
        CurrentVoltage = Measure.GetVoltage();
        CurrentCurrent = Measure.GetCurrent();
    }
}

public sealed class Measurement
{
    // Singleton
    public static readonly Measurement instance = new Measurement();
    public static Measurement GetInstance()
    {
        return instance;
    }

    private Process ProcMeasuring = new Process();
    double measureValue
    bool isOutputObtained;

    private Measurement()
    {
        ProcMeasuring.StartInfo.FileName = "b.exe";
        ProcMeasuring.StartInfo.Arguments = "-c 1";
        ProcMeasuring.StartInfo.WorkingDirectory = Directory.GetCurrentDirectory();
        ProcMeasuring.StartInfo.UseShellExecute = false;
        ProcMeasuring.StartInfo.RedirectStandardInput = true;
        ProcMeasuring.StartInfo.RedirectStandardOutput = true;
        ProcMeasuring.StartInfo.RedirectStandardError = true;
        ProcMeasuring.StartInfo.CreateNoWindow = true;
        ProcMeasuring.OutputDataReceived += new DataReceivedEventHandler(MeasurementOutputHandler);
    }

    public double GetVoltage(Machine machine)
    {
        isOutputObtained = false;

        ProcMeasuring.StandardInput.WriteLine("stat vout2");
        ProcMeasuring.BeginOutputReadLine();

        while (!isOutputObtained)
        {
            isOutputObtained = true;
        }

        return measureValue;
    }

    public double GetCurrent(Machine machine)
    {
        isOutputObtained = false;

        ProcMeasuring.StandardInput.WriteLine("stat cur");
        ProcMeasuring.BeginOutputReadLine();

        while (!isOutputObtained)
        {
            isOutputObtained = true;
        }

        return measureValue;
    }

    private void MeasurementOutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
    {
        if (!String.IsNullOrEmpty(outLine.Data) && (outLine.Data != "# "))
        {                
            measureCurrentValue = Convert.ToDouble(outLine.Data);
            isOutputObtained = true;

            ProcMeasuring.CancelOutputRead();
        }
    }
}
  • 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-03T08:59:59+00:00Added an answer on June 3, 2026 at 8:59 am

    I think you have two options.

    If it is ok to miss a sample because the previous one took too long, then set a flag to indicate that you are already sampling.

    public class TimingController  
    {  
        // Get singleton of Measurement Class  
        Measurement Measure = Measurement.GetInstance();         
    
        bool isMeasuring = false;
    
        public SystemClock ClockInstance  
        {  
            get { return Clock; }  
            set { Clock = value; }  
        }  
    
        private void Measuring()  
        {  
            if(isMeasuring) return;
    
            isMeasuring = true;
            CurrentVoltage = Measure.GetVoltage();  
            CurrentCurrent = Measure.GetCurrent();  
            isMeasuring = false;
        }  
    } 
    

    If it is not ok to miss an interval, you could try creating a new process object for each call rather than reusing the existing process. This could create a lot of children though if the commands take a long time to complete.

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I want use html5's new tag to play a wav file (currently only supported

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.