WebClient.OpenWriteCompleted 事件

定義

在開啟資料流以將資料寫入資源的非同步作業完成時發生。

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 

事件類型

範例

下列程式碼範例示範如何設定此事件的事件處理常式。

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

下列程式碼範例示範這個事件的處理常式實作。

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

備註

每次非同步作業開啟用來將資料傳送至資源完成的資料流程時,都會引發此事件。 這些作業會藉由呼叫 OpenWriteAsync 方法來啟動。

OpenWriteCompletedEventHandler是這個事件的委派。 類別 OpenWriteCompletedEventArgs 會提供事件處理常式與事件資料。

如需如何處理事件的詳細資訊,請參閱 處理和引發事件

適用於