4

基本上我想更新 FormMain (WindowsForm) 上的 ProgressBar UI 对象。我正在使用 .NET 4.0

这是 Form1.Designer.cs 中的代码

namespace ProgressBarApp
{
    public partial class Form1 : Form
    {         
        private System.Windows.Forms.ProgressBar curProgressBar;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            CustomProcess theProcess = new CustomProcess();
            theProcess.Process();
        }
    }
}

这是CustomProcess.cs的定义

namespace ProgressBarApp
{
    class CustomProcess
    {
        public void Process()
        {
            for (int i = 0; i < 10; i++)
            {
                Task ProcessATask = Task.Factory.StartNew(() =>
                    {
                        Thread.Sleep(1000); // simulating a process
                    }
                 );

                Task UpdateProgressBar = ProcessATask.ContinueWith((antecedent) =>
                    { 
                        // how do i update the progress bar object at UI here ?
                    }
                 );
            }
        }
    }
}
4

2 回答 2

6

您可以使用它SynchronizationContext来执行此操作。要将其用于 a Task,您需要创建 a TaskScheduler,您可以通过调用来完成TaskScheduler.FromCurrentSynchronizationContext

Task UpdateProgressBar = ProcessATask.ContinueWith(antecedent =>
    { 
        // you can update the progress bar object here
    }, TaskScheduler.FromCurrentSynchronizationContext());

这仅在您Process()直接从 UI 线程调用时才有效。

于 2012-12-19T10:18:40.153 回答
4

如何使用System.Reactive.Linq

[更新]

using System.Reactive.Linq;

namespace WindowsFormsApplication6
{
    public partial class Form1 : Form
    {
        //private System.Windows.Forms.ProgressBar curProgressBar;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            CustomProcess theProcess = new CustomProcess();
            var x = Observable.FromEventPattern(theProcess, "TaskCompleted");
            curProgressBar.Maximum = 4;
            x.Subscribe((a) =>
            {
                curProgressBar.Value = ((CustomProcess)a.Sender).Counter;
            });
            theProcess.Process();
        }

    }

    class CustomProcess
    {

        public int Counter { get; set; }
        public event EventHandler TaskCompleted = OnTaskCompleted;

        private static void OnTaskCompleted(object sender, EventArgs e)
        {
            ((CustomProcess)sender).Counter++;
        }
        public void Process()
        {

            for (int i = 0; i <= 3; i++)
            {
                Task ProcessATask = Task.Factory.StartNew(() =>
                    {
                        Thread.Sleep(1000); // simulating a process
                    }
                 );
                var awaiter = ProcessATask.GetAwaiter();
                awaiter.OnCompleted(() =>
                {
                    TaskCompleted(this, null);
                });
            }

        }
    }
}
于 2012-12-19T10:30:20.947 回答