James Drake
James Drake

Reputation: 45

Progress bar for an external process

I'm currently writing a lightweight program that consolidates many command line and other external processes into one application.

Currently, I am faced with the challenge of pulling system information using the system info process.

I have successfully coded the button to call the system info process, and redirect the output to a text field.

What I am now attempting is to have a progress bar at the bottom of my WPF window, since it takes a moment to load the system information.

Since I don't know of a way to get an accurate duration from an external process, I am attempting to use the Marquee style.

I've been following examples here on stackoverflow (Windows Forms ProgressBar: Easiest way to start/stop marquee?), as well as other sites, but haven't been able to determine where to put the code so that the progress bar scrolls while systeminfo is running and stops when it is finished.

My current code (without the progressbar1.Style = ProgressBarStyle.Marquee;) is below.

Any suggestions as to where to place the code, or what syntax to use would be greatly appreciated. Thank you in advance!

private void btnSystemInfo_Click(object sender, RoutedEventArgs e)
        {


            // Get system info
            Process info = new Process();
            info.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            info.StartInfo.FileName = "C:\\Windows\\system32\\systeminfo.exe";
            info.StartInfo.Arguments = "-S " + context;
            info.StartInfo.UseShellExecute = false;
            info.StartInfo.CreateNoWindow = true;
            info.StartInfo.RedirectStandardOutput = true;

            info.Start();

            string infoOutput = info.StandardOutput.ReadToEnd();
            info.WaitForExit();

            // Write to the txtInfo text box
            txtInfo.Text = "System Info: " + infoOutput;
            txtInfo.Foreground = Brushes.Black;
            txtInfo.VerticalScrollBarVisibility = ScrollBarVisibility.Visible;

            // Switch to the info tab
            tabControl.SelectedIndex = 3;
        }

Upvotes: 0

Views: 3646

Answers (2)

Paw Baltzersen
Paw Baltzersen

Reputation: 2752

If you have several processes you want to run at the same time, then you should look into the tasking library. You can make the tasks run in parallel or serial, depending on your system resources. You can also keep track of what gets done so you could display a % of work done.

Upvotes: 0

Haris Hasan
Haris Hasan

Reputation: 30097

What you need to do is move the code that gathers system information in the BackgroundWorker thread and in main UI thread start a marquee. Once you get the signal from BackgroundWorker thread that it's work has complete, stop the marquee and display the information in the textBox

void ButtonClickEvent()
{
    BackgroundWorker bg = new BackgroundWorker();
    bg.DoWork += new DoWorkEventHandler(MethodToGetInfo);
    bg.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bg_RunWorkerCompleted);
    //show marquee here
    bg.RunWorkerAsync();
}

void MethodToGetInfo(Object sender, DoWorkEventArgs args)
{
    // find system info here
}

void bg_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs args)
{
    //this method will be called once background worker has completed it's task
    //hide the marquee
    //update the textbox

    //NOTE that this is not a UI thread so you will need BeginInvoke to execute something in the UI thread
}

Upvotes: 4

Related Questions