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。に使用するにはTask、 を作成する必要があります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 に答える