22
20

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

別スレッドからのUI操作についての備忘録

Last updated at Posted at 2017-10-21

簡単なメモ書き。

通常、非同期メソッド内からUI操作をしようとすると
Exceptionが発生します。
以下が回避の方法になります。

別スレッドからのUI操作

private async Task HeavyProcessingAsync()
{
    if (this.InvokeRequied)
    {
        //別スレッドによるUI操作
        this.Invoke((MethodInvoker)(() => TextBox.Text = "hoge"));
    }
    else
    {
        //UIスレッドからのUI操作
        TextBox.Text = "hoge";
    }
}

まずInvokeする必要があるかを判定して、
必要ならばInvokeメソッドとMethodInvokerデリゲートを利用します。


参考:スレッド セーフなコントロールの呼び出し

22
20
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
22
20

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?