Leggi in inglese

Condividi tramite


MethodInvoker Delegato

Definizione

Rappresenta un delegato in grado di eseguire qualsiasi metodo del codice gestito che sia dichiarato void e non utilizzi parametri.

C#
public delegate void MethodInvoker();

Esempio

Nell'esempio di codice seguente viene illustrato come usare un metodo per chiamare un MethodInvoker metodo che aggiorna la barra del titolo del modulo dell'applicazione.

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();
        });
    }
}

Commenti

MethodInvoker fornisce un semplice delegato usato per richiamare un metodo con un elenco di parametri void. Questo delegato può essere usato durante l'esecuzione di chiamate al metodo di Invoke un controllo o quando è necessario un delegato semplice, ma non si vuole definirne uno.

Metodi di estensione

GetMethodInfo(Delegate)

Ottiene un oggetto che rappresenta il metodo rappresentato dal delegato specificato.

Si applica a

Prodotto Versioni
.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, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10