Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Progress Bar for unknown process time

I am developing winform(c#) that start/stop/restart windows services. I want to put a progress bar till the action is completed. I am new to .net programming. Kindly help me on to achieve this.

like image 526
Arjun Avatar asked Jun 15 '11 11:06

Arjun


2 Answers

You cannot show meaningful progress when you don't know how long it takes. You can't know, services take anywhere from 1 to 30 seconds to start. All you can do is show a "I'm not dead, working on it" indicator to the user. ProgressBar supports that, set the Style property to "Marquee".

You'll also need to start the service in a worker thread to avoid your UI from freezing. That's best done with a BackgroundWorker. Make it look similar to this:

public partial class Form1 : Form {
    public Form1() {
        InitializeComponent();
        ServiceProgressBar.Style = ProgressBarStyle.Marquee;
        ServiceProgressBar.Visible = false;
    }

    private void StartButton_Click(object sender, EventArgs e) {
        this.StartButton.Enabled = false;
        this.ServiceProgressBar.Visible = true;
        this.backgroundWorker1.RunWorkerAsync("foo");
    }

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) {
        var ctl = new ServiceController((string)e.Argument);
        ctl.Start();
    }

    private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
        this.StartButton.Enabled = true;
        this.ServiceProgressBar.Visible = false;
        if (e.Error != null) {
            MessageBox.Show(e.Error.ToString(), "Could not start service");
        }
    }
like image 187
Hans Passant Avatar answered Oct 07 '22 19:10

Hans Passant


You must divide up the start/stop/restart progress in small parts and after the part is finished you set the progress bar.
For an instant update you need to get into the methods you are executing to get feedback about its status.

like image 1
CSchulz Avatar answered Oct 07 '22 19:10

CSchulz