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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T21:49:58+00:00 2026-05-19T21:49:58+00:00

I guess you all misunderstood my question and closed it at How to run

  • 0

I guess you all misunderstood my question and closed it at How to run a Command in C# and retrieve data from it?

I had said in that post also :-
want to run a Command from command promt and want its output and maipulate its output. If required, want to close the process and display error or appropriate message. To stop the process, I have to press “F4′ key on command prompt. Till the process is stopeed or killed, it has to be alive only.

I have created a class to handle running the cmd. And I keep getting the output. But on reading the output’s each line I want to stop or throw exception if found anything improper in the output.

I am tying to connect to server via cmd. Server keeps on giving output. Suppose the server gave output as :
Trying to start …..
Cananot load file
…..
Exiting

While retrieving the output, I want to check for lines like “Cannot find file”, “Connected Successfully, etc and set properties ccordingly (like connected = true, errorMsg = “Cannot find file”. Where I am calling the class, I can take care of those proeprties and stop if found connected == true or errorMsg.length > 0. With this inform the user that “Connection is achieved or error msg stating regarding “Cannot load file” and disconnect the server if errorMsg found.

I didn’t find anywhere doing any manipulation on the output receving and that’s where I find myself stuck. I found a lot on internet. Am stuck and trying to figre out this part from last 3-4 days. Then have posted here.

I need help in that. Please help me. If requiried I will psot code snippets. But please help me. AND don’t close this thread as answered ithout understanding my question fully. This is no duplicate.

My code is class :

    public int ConnectToServer()
    {
        int error = 0;
        connected = false;
        try
        {
            process = Process.Start(processInfo);

            process.BeginOutputReadLine();
            process.OutputDataReceived += new DataReceivedEventHandler(Process_OutputDataReceived);
            //if (errorMsg.Length > 0)
            //    throw new Exception(errorMsg);
        }
        catch (Exception e)
        {
            Console.WriteLine("Error Processing ConnectToServer : " + e.Message);
            connected = false;
            errorMsg = e.Message;
            error = -1;
            return error;
        }

        return error;
    }

    private void Process_OutputDataReceived(object sender, DataReceivedEventArgs e)
    {
        errorMsg = "";
        connected = false;
        string d = e.Data;

        if (!string.IsNullOrEmpty(d))
        {
            if (sb != null)
                sb.Append(d + "\n");
            Console.WriteLine("LINE = " + d);
            if (d.IndexOf("Initialization Completed") > 0)
            {
                connected = true;
                Console.WriteLine("********* Connected = " + connected);
            }
            else if (isInValidLine(d))
            {
                //throw new Exception(d);
                connected = false;
                errorMsg = d;
                return;
            }
                            }
        return;
    }

    private bool isInValidLine(string line)
    {
        if (line.IndexOf("Cannot load file") > 0)
        {
            errorMsg = line;
            return true;
        }
        return false;
    }   

IS THE ABOVE CLASS CODE CORRECT WITH MY REQUIREMENTS ?
In impementation :

        while (!oc.Connected)
        {
            timepassed = (int)(DateTime.Now - start).TotalMilliseconds;
            if (timepassed > timeout)
            {
                oc.DisconnectServer();
                connectedToVpn = false;
                throw new Exception("NotConnectedException");
            } else if (oc.ErrorMessage.Length > 0)
            {
                oc.DisconnectServer();
                connectedToVpn = false;
                throw new Exception(oc.ErrorMessage);
            }
            Thread.Sleep(100);
        }   

Here what I am doing is, when I get the output line, I check if it states as Conneced or is invalid. If its invalid, I set the line as the errorMsg. In my while loop I keep chekcing for Connected and errorMessage, but the value of errorMessage stays as “” only. It never gets updated, which tell me that the processing output code is never executed. Nor in debug mode I find the cursor at that line, but the Line = is displayed proeprly in Console. So, don’t understand what’s going wrong and where.

Hope this helps you more understand.

Thanks

  • 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-19T21:49:59+00:00Added an answer on May 19, 2026 at 9:49 pm

    once you have redirected the standard output of the process you have executed you could parse what you receive as it arrives, I believe also line by line, then you can post commands to control your process.

    to read output you have redirected the standard output, to send input you should also redirect the standard input of the process.

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

Sidebar

Related Questions

This is very basic magento question i guess. I want to first get all
So if the original question is tl;dr, I guess all I really need to
I looked all over on google, I guess I just don't know what to
Guess that I have a TextView that I want to update it in some
I guess that the difficult part is the source. Do you know any webpage
I guess getView() is called for each item of the list, but my question
I guess my general question which I googled to no luck: Is there a
I guess the problem is that I do not know how to use factory
I guess my main question is, will this always work as long as I
I have following CSV data: 10,11,12.34 I can parse this using CSV from the

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.