WebClient.OpenWriteCompleted Événement

Définition

Se produit lors d'une opération asynchrone d'ouverture d'un flux pour écrire des données dans une ressource.

public:
 event System::Net::OpenWriteCompletedEventHandler ^ OpenWriteCompleted;
public event System.Net.OpenWriteCompletedEventHandler? OpenWriteCompleted;
public event System.Net.OpenWriteCompletedEventHandler OpenWriteCompleted;
member this.OpenWriteCompleted : System.Net.OpenWriteCompletedEventHandler 
Public Custom Event OpenWriteCompleted As OpenWriteCompletedEventHandler 
Public Event OpenWriteCompleted As OpenWriteCompletedEventHandler 

Type d'événement

Exemples

L’exemple de code suivant montre comment définir un gestionnaire d’événements pour cet événement.

void OpenResourceForWriting2( String^ address )
{
   WebClient^ client = gcnew WebClient;
   Uri ^uri = gcnew Uri(address);

   // Specify that the OpenWriteCallback method gets called
   // when the writeable stream is available.
   client->OpenWriteCompleted += gcnew OpenWriteCompletedEventHandler( OpenWriteCallback2 );
   client->OpenWriteAsync( uri, "POST" );

   // Applications can perform other tasks
   // while waiting for the upload to complete.
}
public static void OpenResourceForWriting2(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, "POST");
    // Applications can perform other tasks
    // while waiting for the upload to complete.
}
Public Shared Sub OpenResourceForWriting2(ByVal address As String)

    Dim client As WebClient = New WebClient()

    '  Specify that the OpenWriteCallback method gets called
    '  when the writeable stream is available.
    AddHandler client.OpenWriteCompleted, AddressOf OpenWriteCallback2
                Dim uri as Uri = New Uri(address)
    client.OpenWriteAsync(uri, "POST")
    '  Applications can perform other tasks
    '  while waiting for the upload to complete.
End Sub

L’exemple de code suivant montre une implémentation d’un gestionnaire pour cet événement.

void OpenWriteCallback2( Object^ /*sender*/, OpenWriteCompletedEventArgs^ e )
{
   Stream^ body = nullptr;
   StreamWriter^ s = nullptr;
   try
   {
      body = dynamic_cast<Stream^>(e->Result);
      s = gcnew StreamWriter( body );
      s->AutoFlush = true;
      s->Write( "This is content data to be sent to the server." );
   }
   finally
   {
      if ( s != nullptr )
      {
         s->Close();
      }
      if ( body != nullptr )
      {
         body->Close();
      }
   }

}
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();
        }
    }
}
Private Shared Sub OpenWriteCallback2(ByVal sender As Object, ByVal e As OpenWriteCompletedEventArgs)

    Dim body As Stream = Nothing
    Dim s As StreamWriter = Nothing

    Try

        body = CType(e.Result, Stream)
        s = New StreamWriter(body)
        s.AutoFlush = True
        s.Write("This is content data to be sent to the server.")
    Finally

        If Not s Is Nothing Then

            s.Close()
        End If

        If Not body Is Nothing Then

            body.Close()
        End If
    End Try
End Sub

Remarques

Cet événement est déclenché chaque fois qu’une opération asynchrone d’ouverture d’un flux utilisé pour envoyer des données à une ressource se termine. Ces opérations sont démarrées en appelant les OpenWriteAsync méthodes.

OpenWriteCompletedEventHandler est le délégué de cet événement. La OpenWriteCompletedEventArgs classe fournit au gestionnaire d’événements des données d’événement.

Pour plus d’informations sur la façon de gérer les événements, consultez gestion et déclenchement d’événements.

S’applique à