Leer en inglés

Compartir a través de


MethodInvoker Delegado

Definición

Representa un delegado que puede ejecutar cualquier método en código administrado que se declara como void y no toma ningún parámetro.

C#
public delegate void MethodInvoker();

Ejemplos

En el ejemplo de código siguiente se muestra cómo usar para MethodInvoker llamar a un método que actualiza la barra de título del formulario de aplicación.

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

Comentarios

MethodInvoker proporciona un delegado simple que se usa para invocar un método con una lista de parámetros void. Este delegado se puede usar al realizar llamadas al método de Invoke un control, o cuando se necesita un delegado simple, pero no desea definir uno usted mismo.

Métodos de extensión

GetMethodInfo(Delegate)

Obtiene un objeto que representa el método representado por el delegado especificado.

Se aplica a

Producto Versiones
.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