MethodInvoker MethodInvoker MethodInvoker MethodInvoker Delegate

定义

表示一个委托,该委托可执行托管代码中声明为 void 且不接受任何参数的任何方法。Represents a delegate that can execute any method in managed code that is declared void and takes no parameters.

public delegate void MethodInvoker();
public delegate void MethodInvoker();
type MethodInvoker = delegate of unit -> unit
Public Delegate Sub MethodInvoker()
继承
MethodInvokerMethodInvokerMethodInvokerMethodInvoker

示例

下面的代码示例演示如何使用MethodInvoker来调用一个方法, 该方法可更新应用程序窗体的标题栏。The following code example demonstrates how to use a MethodInvoker to call a method that updates the title bar of the application form.

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 参数列表的方法。MethodInvoker provides a simple delegate that is used to invoke a method with a void parameter list. 当调用控件的Invoke方法时, 或者当你需要简单的委托但又不想自行定义时, 可以使用此委托。This delegate can be used when making calls to a control's Invoke method, or when you need a simple delegate but do not want to define one yourself.

扩展方法

GetMethodInfo(Delegate) GetMethodInfo(Delegate) GetMethodInfo(Delegate) GetMethodInfo(Delegate)

获取指示指定委托表示的方法的对象。Gets an object that represents the method represented by the specified delegate.

适用于