Update UI from background Thread

后端 未结 6 1329
悲&欢浪女
悲&欢浪女 2020-12-20 10:33

This is just a curious question. Which one is the best way to update UI from another thread. First, this one:

private delegate void MyDelegateMethod();
void          


        
6条回答
  •  半阙折子戏
    2020-12-20 10:52

    The default Action delegate worked 90% of the time:

    private void Log(String value)
    {
        // Verify that we're on the same thread
        if (textBox1.InvokeRequired)
        {
            // We're not on the same thread - Invoke the UI thread
            textBox1.Invoke(new Action(Log), value);
            return;
        }
    
        // We're on the same thread - Update UI
        textBox1.Text += value + "\r\n";
    }
    
    private void OnSomethingHappened()
    {
        Log("Something happened!");
    }
    

提交回复
热议问题