MethodInvoker 委托
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示一个委托,该委托可执行托管代码中声明为 void 且不接受任何参数的任何方法。
public delegate void MethodInvoker();
C#
public delegate void MethodInvoker();
type MethodInvoker = delegate of unit -> unit
Public Delegate Sub MethodInvoker()
下面的代码示例演示如何使用方法来 MethodInvoker 调用更新应用程序窗体的标题栏的方法。
C#
public partial class Form1 : Form
{
public Form1()
{
// Create a timer that will call the ShowTime method every second.
var timer = new System.Threading.Timer(ShowTime, null, 0, 1000);
}
private void ShowTime(object x)
{
// Don't do anything if the form's handle hasn't been created
// or the form has been disposed.
if (!this.IsHandleCreated || this.IsDisposed) return;
// Invoke an anonymous method on the thread of the form.
this.Invoke((MethodInvoker) delegate
{
// Show the current time in the form's title bar.
this.Text = DateTime.Now.ToLongTimeString();
});
}
}
Partial Public Class Form1
Inherits System.Windows.Forms.Form
Public Sub New()
' Create a timer that will call the ShowTime method every second.
Dim timer As System.Threading.Timer = _
New System.Threading.Timer(AddressOf ShowTime, Nothing, 0, 1000)
End Sub
Sub ShowTime(ByVal x As Object)
' Don't do anything if the form's handle hasn't been created
' or the form has been disposed.
If (Not Me.IsHandleCreated OrElse Me.IsDisposed) Then Return
' Create the method invoker.
' The method body shows the current time in the forms title bar.
Dim mi As MethodInvoker = AddressOf UpdateFormText
Me.Invoke(mi)
End Sub
Sub UpdateFormText()
Me.Text = DateTime.Now.ToLongTimeString()
End Sub
End Class
MethodInvoker 提供一个简单的委托,用于使用 void 参数列表调用方法。 在调用控件 Invoke 的方法或需要简单委托但不想自行定义时,可以使用此委托。
Get |
获取指示指定委托表示的方法的对象。 |
产品 | 版本 |
---|---|
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8 |
Windows Desktop | 3.0, 3.1, 5, 6, 7 |