Leer en inglés

Compartir a través de


OpenWriteCompletedEventHandler Delegado

Definición

Representa el método que controlará el evento OpenWriteCompleted de WebClient.

C#
public delegate void OpenWriteCompletedEventHandler(object sender, OpenWriteCompletedEventArgs e);

Parámetros

sender
Object

Origen del evento.

e
OpenWriteCompletedEventArgs

OpenWriteCompletedEventArgs que contiene los datos del evento.

Ejemplos

En el ejemplo de código siguiente se muestra cómo abrir un flujo para escribir datos que se van a cargar.

C#
public static void OpenResourceForPosting(string address)
{
    WebClient client = new WebClient();
    Uri uri = new Uri(address);

    // Specify that the OpenWriteCallback method gets called
    // when the writeable stream is available.
    client.OpenWriteCompleted += new OpenWriteCompletedEventHandler(OpenWriteCallback2);
    client.OpenWriteAsync(uri);
    // Applications can perform other tasks
    // while waiting for the upload to complete.
}

Se llama al método siguiente cuando la secuencia está disponible.

C#
private static void OpenWriteCallback2(Object sender, OpenWriteCompletedEventArgs e)
{
    Stream body = null;
    StreamWriter s = null;

    try
    {
        body = (Stream)e.Result;
        s = new StreamWriter(body);
        s.AutoFlush = true;
        s.Write("This is content data to be sent to the server.");
    }
    finally
    {
        if (s != null)
        {
            s.Close();
        }

        if (body != null)
        {
            body.Close();
        }
    }
}

Comentarios

Cuando se crea un delegado OpenWriteCompletedEventHandler, se identifica el método que controlará el evento. Para asociar el evento al controlador, se debe agregar una instancia del delegado al evento. Siempre que se produce el evento, se llama a su controlador, a menos que se quite el delegado.

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 Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 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
.NET Standard 2.0, 2.1