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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:58:06+00:00 2026-05-17T23:58:06+00:00

I know little about C#. I am trying to display a progressbar with the

  • 0

I know little about C#.

I am trying to display a progressbar with the status of the background command line program.

After google examples of progressbar and run process in c#, so I’m trying to start a BackgroundWorker in the Windows Form, and run the command line program in the BackgroundWorker. I want to parse the command line’s output to get the progress
percentage and display it to the progress bar.

The command line program is a console program, it will output periodically its current status of progress.

But it seems it did not work. I can’t readline any lines of the process’s standard output.

Is there anything I missed?

Here is the raw code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;
using System.Threading;
using System.Diagnostics;
using System.IO;


public class DemoForm : Form
{
    private BackgroundWorker backgroundWorker1;
    private Button loadButton;
    private ProgressBar progressBar1;

    public DemoForm()
    {
        InitializeComponent();

        backgroundWorker1 = new System.ComponentModel.BackgroundWorker();
        backgroundWorker1.DoWork += new System.ComponentModel.DoWorkEventHandler(
                this.backgroundWorker1_DoWork);
        backgroundWorker1.RunWorkerCompleted += 
            new System.ComponentModel.RunWorkerCompletedEventHandler(
                    this.backgroundWorker1_RunWorkerCompleted);
        backgroundWorker1.ProgressChanged += 
            new System.ComponentModel.ProgressChangedEventHandler(
                    this.backgroundWorker1_ProgressChanged);

    }

    private void loadButton_Click(object sender, EventArgs e)
    {
        backgroundWorker1.RunWorkerAsync();

        this.loadButton.Enabled = false;
    }

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        string status;
        Process p = new Process();
        p.StartInfo.FileName = "xx.exe";
        p.StartInfo.Arguments = "-q -r test.cap -p";
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.RedirectStandardOutput = true;
        p.StartInfo.CreateNoWindow = true;
        p.OutputDataReceived += this.p_OutputDataReceived;
        p.EnableRaisingEvents = true;
        p.Start();
        p.BeginOutputReadLine();

/*
        while((status = p.StandardOutput.ReadLine()) != null)
        {
            Console.WriteLine(status);
            string[] words = status.Split(' ');
            int offset = Convert.ToInt32(words[0]);
            int size   = Convert.ToInt32(words[1]);

            int percentComplete = 100 * offset / size;
            MessageBox.Show(words[0], words[1]);
            backgroundWorker1.ReportProgress(percentComplete);
        }
        Console.WriteLine("wait for exit!");
        StreamReader myStreamReader = p.StandardOutput;
        status = myStreamReader.ReadLine();
        Console.WriteLine(status);
        Console.WriteLine("wait for exit!");
        */
        p.WaitForExit();
    }

    void p_OutputDataReceived(object sender, DataReceivedEventArgs e)
    {
        string status = e.Data;
        Console.WriteLine("get events");
        Console.WriteLine(status);
        string[] words = status.Split(' ');
        int offset = Convert.ToInt32(words[0]);
        int size   = Convert.ToInt32(words[1]);

        int percentComplete = 100 * offset / size;
        MessageBox.Show(words[0], words[1]);
        backgroundWorker1.ReportProgress(percentComplete);
    }

    private void backgroundWorker1_RunWorkerCompleted(object sender,
            RunWorkerCompletedEventArgs e)
    {
        progressBar1.Value = 100;
        if (e.Error == null) {
            MessageBox.Show ("Demo", "Loading completed");
        }
    }

    private void backgroundWorker1_ProgressChanged(object sender,
            ProgressChangedEventArgs e)
    {
        this.progressBar1.Value = e.ProgressPercentage;
    }

    private System.ComponentModel.IContainer components = null;

    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    private void InitializeComponent()
    {
        this.loadButton = new System.Windows.Forms.Button();
        this.progressBar1 = new System.Windows.Forms.ProgressBar();

        /* load button */
        this.loadButton.Location = new System.Drawing.Point(12, 12);
        this.loadButton.Name = "loadButton";
        this.loadButton.Size = new System.Drawing.Size(100, 23);
        this.loadButton.TabIndex = 0;
        this.loadButton.Text = "Load file";
        this.loadButton.UseVisualStyleBackColor = true;
        this.loadButton.Click += new System.EventHandler(this.loadButton_Click);

        /* progress bar */
        this.progressBar1.Location = new System.Drawing.Point(12, 50);
        this.progressBar1.Name = "progressBar1";
        this.progressBar1.Size = new System.Drawing.Size(100, 26);
        this.progressBar1.TabIndex = 1;

        /* Form */
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(133, 104);
        this.Controls.Add(this.progressBar1);
        this.Controls.Add(this.loadButton);
        this.Name = "DemoForm";
        this.Text = "DemoForm";
        this.ResumeLayout (false);
    }
}

static class Program
{
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.Run(new DemoForm());
    }
}

Or is there any better way to get a command line program’s progress?

  • 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-17T23:58:07+00:00Added an answer on May 17, 2026 at 11:58 pm

    After manually flush the output of the command line program, problem solved.

    fflush(stdout);
    

    It’s not clear why it’s buffered even a line ended.

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

Sidebar

Related Questions

I really know very little about SVN and I'm trying to learn what it's
Ok I know little about SQL so bear with me... I'm trying to see
I'm trying to set up authentication on my server, however, I know little about
I know very little about JavaScript but despite this I'm trying to cobble something
I really know very little about regex's. I'm trying to test a password validation.
I know very little about encryption/hashing. I have to hash an encryption key. The
I know very little about Flash, and so is not programming in it. I
Pardon me if this has already been asked (I know very little about Data
The code here is X++. I know very little about it, though I am
First, please be gentle, I know very little about DB design. I am working

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.