MethodInvoker 대리자

정의

관리 코드에서 void로 선언되고 매개 변수를 사용하지 않는 모든 메서드를 실행할 수 있는 대리자를 나타냅니다.

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

예제

다음 코드 예제를 사용 하는 방법에 설명 된 MethodInvoker 애플리케이션 폼의 제목 표시줄을 업데이트 하는 메서드를 호출 합니다.

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 메서드 또는 대리자 하지만 직접 정의 하지 않으려는 간단한 해야 합니다.

확장 메서드

GetMethodInfo(Delegate)

지정된 대리자가 나타내는 메서드를 나타내는 개체를 가져옵니다.

적용 대상