首页 >> VS.NET

C#如何从线程中操作控件

2012-06-26 10:11:03

调用涉及其他线程所拥有的控件的方法

  1. 用与要调用的方法相同的签名声明一个委托。

    下面的示例显示如何使用 Integer 和 String 参数声明委托。

    VB
    PublicDelegateSubmyDelegate(ByValanIntegerasInteger,ByVal_ aStringasString)

    C#
    publicdelegatevoidmyDelegate(intanInteger,stringaString);
  2. 使用任何控件来调用对其他线程所拥有的控件进行操作的方法。

    • 如果要同步调用方法,请调用 Control.Invoke 方法

      VB
      Label1.Invoke(NewmyDelegate(AddressOfmyMethod),New_Object() {1,"This is the string"})


      C#
      Label1.Invoke(newmyDelegate(myMethod),newObject[] {1,"This is the string"});
    • 如果要异步调用方法,请调用 Control.BeginInvoke 方法

      VB
      Label1.BeginInvoke(NewmyDelegate(AddressOfmyMethod), _NewObject() {1,"This is the string"})


      C#
      Label1.BeginInvoke(newmyDelegate(myMethod),newObject[] {1,"This is the string"});