MethodInvoker Délégué

Définition

Représente un délégué qui peut exécuter toute méthode dans un code managé qui est déclaré void et n'accepte aucun paramètre.

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

Exemples

L’exemple de code suivant montre comment utiliser une méthode pour appeler une MethodInvoker méthode qui met à jour la barre de titre du formulaire d’application.

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

Remarques

MethodInvoker fournit un délégué simple utilisé pour appeler une méthode avec une liste de paramètres void. Ce délégué peut être utilisé lors de l’appel à la méthode d’un Invoke contrôle ou lorsque vous avez besoin d’un délégué simple, mais ne souhaitez pas en définir vous-même.

Méthodes d’extension

GetMethodInfo(Delegate)

Obtient un objet qui représente la méthode représentée par le délégué spécifié.

S’applique à