日期:2014-05-20  浏览次数:20975 次

一种奇怪的写法,求解答
private void m_OnOccurErr(int errCode)
  {
  this.SafeInvoke(() =>
  {
  switch (errCode)
  {
  case 1:
  textBox4.Text = "等待超时错误!";
  break;
  case 2:
  textBox4.Text = "获取状态错误!";
  break;
  default:
  break;
  }

  });
  }




public delegate void InvokeHandler();

  public static void SafeInvoke(this Control control, InvokeHandler handler)
  {
  if (control.InvokeRequired)
  {
  control.Invoke(handler);
  }
  else
  {
  handler();
  }
  }

------解决方案--------------------
那里奇怪啊,挺正常滴

C# code
public delegate void InvokeHandler();


  public static void SafeInvoke(this Control control, InvokeHandler handler)//此处是扩展方法,对Control对象扩展一个safeInvoke方法,InvokeHandler则是委托
  {
  if (control.InvokeRequired)
  {
  control.Invoke(handler);
  }
  else
  {
  handler();
  }
  }