简体   繁体   中英

Update progress bar in another form while task is running

**Ultimately I am going to have four tasks running concurrently and have another form that contains four progress bars. I would like for each progress bar to update as it's work task is completing.

Here's what I'm trying to do for starters.

I have a form that has some buttons on it. When I click one I'm creating a new task to do some work.

public partial class MyMainForm : Form
{

    private void btn_doWork_Click(object sender, EventArgs e)
    {
        Task task = new Task(RunComparisons);
        task.Start();
    }

    private void RunComparisons()
    {
        int progressBarValue = 0;
        MyProgressBarForm pBar = new MyProgressBarForm(maxValue, "some text");
        pBar.ShowDialog();
        foreach(string s in nodeCollection)
        {
            //do some work here
            progressBarValue++;
            pBar.updateProgressBar(progressBarValue, "some new text");
        }
        pBar.BeginInvoke(new Action(() => pBar.Close()));
    }
}

In another class that contains a form with a progress bar:

public partial class MyProgressBarForm : Form
{
    public MyProgressBarForm(int maxValue, string textToDisplay)
    {
        InitializeComponent();
        MyProgressBarControl.Maximum = maxValue;
        myLabel.Text = textToDisplay;
    }

    public void updateProgressBar(int progress, string updatedTextToDisplay)
    {
        MyProgressBarForm.BeginInvoke(
            new Action(() =>
            {
                MyProgressBarControl.Value = progress;
                myLabel.Text = updatedTextToDisplay;
            }));
    }

When I click the doWork button the progress bar form displays but doesn't update. It just sits there and hangs. If I comment out the pBar.ShowDialog(); then the progress bar form doesn't display but the work to be done is run to completion perfectly.

I had this working perfectly when I was creating my own threads but I read about Tasks and now I'm trying to get this to run that way. Where did I go wrong?

**Ultimately I am going to have four tasks running concurrently and have another form that contains four progress bars. I would like for each progress bar to update as it's work task is completing.

Here's what I'm trying to do for starters.

I have a form that has some buttons on it. When I click one I'm creating a new task to do some work.

public partial class MyMainForm : Form
{

    private void btn_doWork_Click(object sender, EventArgs e)
    {
        Task task = new Task(RunComparisons);
        task.Start();
    }

    private void RunComparisons()
    {
        int progressBarValue = 0;
        MyProgressBarForm pBar = new MyProgressBarForm(maxValue, "some text");
        pBar.ShowDialog();
        foreach(string s in nodeCollection)
        {
            //do some work here
            progressBarValue++;
            pBar.updateProgressBar(progressBarValue, "some new text");
        }
        pBar.BeginInvoke(new Action(() => pBar.Close()));
    }
}

In another class that contains a form with a progress bar:

public partial class MyProgressBarForm : Form
{
    public MyProgressBarForm(int maxValue, string textToDisplay)
    {
        InitializeComponent();
        MyProgressBarControl.Maximum = maxValue;
        myLabel.Text = textToDisplay;
    }

    public void updateProgressBar(int progress, string updatedTextToDisplay)
    {
        MyProgressBarForm.BeginInvoke(
            new Action(() =>
            {
                MyProgressBarControl.Value = progress;
                myLabel.Text = updatedTextToDisplay;
            }));
    }

When I click the doWork button the progress bar form displays but doesn't update. It just sits there and hangs. If I comment out the pBar.ShowDialog(); then the progress bar form doesn't display but the work to be done is run to completion perfectly.

I had this working perfectly when I was creating my own threads but I read about Tasks and now I'm trying to get this to run that way. Where did I go wrong?

**Ultimately I am going to have four tasks running concurrently and have another form that contains four progress bars. I would like for each progress bar to update as it's work task is completing.

Here's what I'm trying to do for starters.

I have a form that has some buttons on it. When I click one I'm creating a new task to do some work.

public partial class MyMainForm : Form
{

    private void btn_doWork_Click(object sender, EventArgs e)
    {
        Task task = new Task(RunComparisons);
        task.Start();
    }

    private void RunComparisons()
    {
        int progressBarValue = 0;
        MyProgressBarForm pBar = new MyProgressBarForm(maxValue, "some text");
        pBar.ShowDialog();
        foreach(string s in nodeCollection)
        {
            //do some work here
            progressBarValue++;
            pBar.updateProgressBar(progressBarValue, "some new text");
        }
        pBar.BeginInvoke(new Action(() => pBar.Close()));
    }
}

In another class that contains a form with a progress bar:

public partial class MyProgressBarForm : Form
{
    public MyProgressBarForm(int maxValue, string textToDisplay)
    {
        InitializeComponent();
        MyProgressBarControl.Maximum = maxValue;
        myLabel.Text = textToDisplay;
    }

    public void updateProgressBar(int progress, string updatedTextToDisplay)
    {
        MyProgressBarForm.BeginInvoke(
            new Action(() =>
            {
                MyProgressBarControl.Value = progress;
                myLabel.Text = updatedTextToDisplay;
            }));
    }

When I click the doWork button the progress bar form displays but doesn't update. It just sits there and hangs. If I comment out the pBar.ShowDialog(); then the progress bar form doesn't display but the work to be done is run to completion perfectly.

I had this working perfectly when I was creating my own threads but I read about Tasks and now I'm trying to get this to run that way. Where did I go wrong?

**Ultimately I am going to have four tasks running concurrently and have another form that contains four progress bars. I would like for each progress bar to update as it's work task is completing.

Here's what I'm trying to do for starters.

I have a form that has some buttons on it. When I click one I'm creating a new task to do some work.

public partial class MyMainForm : Form
{

    private void btn_doWork_Click(object sender, EventArgs e)
    {
        Task task = new Task(RunComparisons);
        task.Start();
    }

    private void RunComparisons()
    {
        int progressBarValue = 0;
        MyProgressBarForm pBar = new MyProgressBarForm(maxValue, "some text");
        pBar.ShowDialog();
        foreach(string s in nodeCollection)
        {
            //do some work here
            progressBarValue++;
            pBar.updateProgressBar(progressBarValue, "some new text");
        }
        pBar.BeginInvoke(new Action(() => pBar.Close()));
    }
}

In another class that contains a form with a progress bar:

public partial class MyProgressBarForm : Form
{
    public MyProgressBarForm(int maxValue, string textToDisplay)
    {
        InitializeComponent();
        MyProgressBarControl.Maximum = maxValue;
        myLabel.Text = textToDisplay;
    }

    public void updateProgressBar(int progress, string updatedTextToDisplay)
    {
        MyProgressBarForm.BeginInvoke(
            new Action(() =>
            {
                MyProgressBarControl.Value = progress;
                myLabel.Text = updatedTextToDisplay;
            }));
    }

When I click the doWork button the progress bar form displays but doesn't update. It just sits there and hangs. If I comment out the pBar.ShowDialog(); then the progress bar form doesn't display but the work to be done is run to completion perfectly.

I had this working perfectly when I was creating my own threads but I read about Tasks and now I'm trying to get this to run that way. Where did I go wrong?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM