TPLタスクを介して別のスレッドで長期実行プロセスを実行する簡単なwinformsアプリケーションがあります。この長時間実行中のプロセスでは、UI(進行状況バーなど)を更新したいと考えています。 .ContinueWith()に必須でなくてもこれを行う方法はありますか?このコードを実行するWinFormsの子タスクからUIを更新する方法
public partial class Form1 : Form
{
private Task _childTask;
public Form1()
{
InitializeComponent();
Task.Factory.StartNew(() =>
{
// Do some work
Thread.Sleep(1000);
// Update the UI
_childTask.Start();
// Do more work
Thread.Sleep(1000);
});
_childTask = new Task((antecedent) =>
{
Thread.Sleep(2000);
textBox1.Text = "From child task";
}, TaskScheduler.FromCurrentSynchronizationContext());
}
}
私はユビキタス例外を取得:
Cross-thread operation not valid: Control 'textBox1' accessed from a thread other than the thread it was created on.
スレッドにテキストボックスの参照を貼り付ける必要があると思います。 – jwillmer