当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - Newbie need some help on threading

问题描述:

This question already has an answer here:

  • Cross-thread operation not valid: Control accessed from a thread other than the thread it was created on

    15 answers

网友答案:

You should consider using the Task Parallel Library, creating a Thread explicitly is considered legacy code nowadays. However, to fix your code, you need to invoke the UI updates on the UI Thread.

public void Fill()
{
   Thread.Sleep(5000);

   Action doUIWork = () => 
   {
      ComboBox1.Items.Add(1);
      ComboBox1.Items.Add(2);
      ComboBox1.Items.Add(3);
      ComboBox1.Items.Add(4);
      ComboBox1.Items.Add(5);
      ComboBox1.Items.Add(6);
      ComboBox1.Items.Add(7);
   };

   this.Invoke(doUIWork);
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: