C#通过委托处理 控件

来源:互联网 发布:mac怎么阻止弹出网页 编辑:程序博客网 时间:2024/05/17 22:34
  1.   public Form1()
  2.         {
  3.             InitializeComponent();
  4.             listBoxCallback = new AddListBoxItemCallback(AddListBoxItem);
  5.         }
  6.       // string senddate,readdate;
  7.        
  8.       // NetworkStream ns;
  9.         delegate void AddListBoxItemCallback(string text);
  10.         AddListBoxItemCallback listBoxCallback;
  11.         private void AddListBoxItem(string text)
  12.         {
  13.             //如果listBoxReceive被不同的线程访问则通过委托处理;
  14.             if (listBoxReceive.InvokeRequired)
  15.             {
  16.                 this.Invoke(listBoxCallback, text);
  17.             }
  18.             else
  19.             {
  20.                 listBoxReceive.Items.Add(text);
  21.                 listBoxReceive.SelectedIndex = listBoxReceive.Items.Count - 1;
  22.             }
  23.         }
  • 原创粉丝点击