crossthread operations error

后端 未结 3 756
礼貌的吻别
礼貌的吻别 2021-01-25 21:43
      if (listBox1.InvokeRequired)
       {
           listBox = new StringBuilder(this.listBox1.Text);
       }

This is the code in c# which when exec

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-25 22:21

    Your code should run when InvokeRequired is false

    delegate void SetListBoxDelegate(); 
    
    void SetListBox()
    {
        if(!InvokeRequired)
        {
            listBox = new StringBuilder(this.listBox1.Text);
        } 
        else 
            Invoke(new SetListBoxDelegate(SetListBox)); 
    } 
    

    Edit: Check out Making Windows Forms thread safe

提交回复
热议问题