且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

***的方式来调用任何交叉线程code?

更新时间:2022-12-31 08:40:36

您也可以使用扩展方法和lambda表达式,让您的code干净多了。

You also could use an extension method and lambdas to make your code much cleaner.

using System.ComponentModel;
public static class ISynchronizeInvokeExtensions
{
  public static void InvokeEx<T>(this T @this, Action<T> action) where T : ISynchronizeInvoke
  {
    if (@this.InvokeRequired)
    {
      @this.Invoke(action, new object[] { @this });
    }
    else
    {
      action(@this);
    }
  }
}

所以,现在你可以使用 InvokeEx 上的任何ISynchronizeInvoke,并能够访问实现类的属性和字段。

So now you can use InvokeEx on any ISynchronizeInvoke and be able to access the properties and fields of implementing class.

this.InvokeEx(f => f.listView1.Items.Clear());