简体   繁体   English

在任务运行时以另一种形式更新进度栏

[英]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.当我单击doWork按钮时,进度条表单将显示,但不会更新。 It just sits there and hangs.它只是坐在那里并挂起。 If I comment out the pBar.ShowDialog();如果我注释掉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.在创建自己的线程时,我可以完美地工作,但是我了解了Tasks,现在我正在尝试使其以这种方式运行。 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.当我单击doWork按钮时,进度条表单将显示,但不会更新。 It just sits there and hangs.它只是坐在那里并挂起。 If I comment out the pBar.ShowDialog();如果我注释掉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.在创建自己的线程时,我可以完美地工作,但是我了解了Tasks,现在我正在尝试使其以这种方式运行。 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.当我单击doWork按钮时,进度条表单将显示,但不会更新。 It just sits there and hangs.它只是坐在那里并挂起。 If I comment out the pBar.ShowDialog();如果我注释掉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.在创建自己的线程时,我可以完美地工作,但是我了解了Tasks,现在我正在尝试使其以这种方式运行。 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.当我单击doWork按钮时,进度条表单将显示,但不会更新。 It just sits there and hangs.它只是坐在那里并挂起。 If I comment out the pBar.ShowDialog();如果我注释掉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.在创建自己的线程时,我可以完美地工作,但是我了解了Tasks,现在我正在尝试使其以这种方式运行。 Where did I go wrong?我哪里做错了?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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