サーチ…


備考

タスクパラレルライブラリには、アプリケーションに並列性と並行性を追加するプロセスを大幅に簡素化するパブリックタイプとAPIが用意されています。 。ネット。 TPLは.Net 4で導入され、マルチスレッドと並列コードを書くための推奨される方法です。

TPLは、作業スケジューリング、スレッドアフィニティ、キャンセルのサポート、状態管理、ロードバランシングを行い、プログラマが一般的な低レベルの詳細に時間を費やすのではなく、問題の解決に集中できるようにします。

ボタンのクリックに応じて作業を実行し、UIを更新する

この例では、ワーカースレッドで何らかの作業を実行してボタンクリックに応答し、ユーザーインターフェイスを更新して完了を示す方法を示します

void MyButton_OnClick(object sender, EventArgs args)
{
    Task.Run(() => // Schedule work using the thread pool
        {
            System.Threading.Thread.Sleep(5000); // Sleep for 5 seconds to simulate work.
        })
    .ContinueWith(p => // this continuation contains the 'update' code to run on the UI thread
    {
        this.TextBlock_ResultText.Text = "The work completed at " + DateTime.Now.ToString()
    },
    TaskScheduler.FromCurrentSynchronizationContext()); // make sure the update is run on the UI thread.

}


Modified text is an extract of the original Stack Overflow Documentation
ライセンスを受けた CC BY-SA 3.0
所属していない Stack Overflow