共用方式為


OpenReadCompletedEventHandler 代理人

定義

代表將處理 OpenReadCompletedWebClient 事件的方法。

public delegate void OpenReadCompletedEventHandler(System::Object ^ sender, OpenReadCompletedEventArgs ^ e);
public delegate void OpenReadCompletedEventHandler(object sender, OpenReadCompletedEventArgs e);
type OpenReadCompletedEventHandler = delegate of obj * OpenReadCompletedEventArgs -> unit
Public Delegate Sub OpenReadCompletedEventHandler(sender As Object, e As OpenReadCompletedEventArgs)

參數

sender
Object

事件的來源。

範例

下列程式代碼範例示範下載要讀取的資源。

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

   client->OpenReadCompleted += gcnew OpenReadCompletedEventHandler( OpenReadCallback2 );
   client->OpenReadAsync( uri );
}
public static void OpenResourceForReading2(string address)
{
    WebClient client = new WebClient();
    Uri uri = new Uri(address);

    client.OpenReadCompleted += new OpenReadCompletedEventHandler(OpenReadCallback2);
    client.OpenReadAsync(uri);
}
Public Shared Sub OpenResourceForReading2(ByVal address As String)

    Dim client As WebClient = New WebClient()
    AddHandler client.OpenReadCompleted, AddressOf OpenReadCallback2
                Dim uri as Uri = New Uri(address)
    client.OpenReadAsync(uri)
End Sub

下載完成時會呼叫下列方法。

void OpenReadCallback2( Object^ /*sender*/, OpenReadCompletedEventArgs^ e )
{
   Stream^ reply = nullptr;
   StreamReader^ s = nullptr;
   try
   {
      reply = dynamic_cast<Stream^>(e->Result);
      s = gcnew StreamReader( reply );
      Console::WriteLine( s->ReadToEnd() );
   }
   finally
   {
      if ( s != nullptr )
      {
         s->Close();
      }
      if ( reply != nullptr )
      {
         reply->Close();
      }
   }

}
private static void OpenReadCallback2(Object sender, OpenReadCompletedEventArgs e)
{
    Stream reply = null;
    StreamReader s = null;

    try
    {
        reply = (Stream)e.Result;
        s = new StreamReader(reply);
        Console.WriteLine(s.ReadToEnd());
    }
    finally
    {
        if (s != null)
        {
            s.Close();
        }

        if (reply != null)
        {
            reply.Close();
        }
    }
}
Private Shared Sub OpenReadCallback2(ByVal sender As Object, ByVal e As OpenReadCompletedEventArgs)

    Dim reply As Stream = Nothing
    Dim s As StreamReader = Nothing

    Try

        reply = CType(e.Result, Stream)
        s = New StreamReader(reply)
        Console.WriteLine(s.ReadToEnd())
    Finally

        If Not s Is Nothing Then

            s.Close()
        End If

        If Not reply Is Nothing Then

            reply.Close()
        End If
    End Try
End Sub

備註

建立 OpenReadCompletedEventHandler 委派時,必須識別處理事件的方法。 若要使事件與您的事件處理常式產生關聯,請將委派的執行個體 (Instance) 加入至事件。 除非您移除委派,否則每當事件發生時就會呼叫事件處理常式。

擴充方法

GetMethodInfo(Delegate)

取得表示特定委派所代表之方法的物件。

適用於