OpenReadCompletedEventHandler Delegát

Definice

Představuje metodu, která zpracuje OpenReadCompleted událost WebClient .Represents the method that will handle the OpenReadCompleted event of a WebClient.

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)

Parametry

sender
Object

Zdroj událostiThe source of the event.

e
OpenReadCompletedEventArgs

OpenReadCompletedEventArgsObsahující data události.A OpenReadCompletedEventArgs containing event data.

Příklady

Následující příklad kódu ukazuje stažení prostředku pro čtení.The following code example demonstrates downloading a resource for reading.

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

Následující metoda je volána po dokončení stahování.The following method is called when the download completes.

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

Poznámky

Při vytváření OpenReadCompletedEventHandler delegáta identifikujete metodu, která událost zpracuje.When you create a OpenReadCompletedEventHandler delegate, you identify the method that will handle the event. Chcete-li přidružit událost k obslužné rutině události, přidejte do události instanci delegáta.To associate the event with your event handler, add an instance of the delegate to the event. Obslužná rutina události je volána při každém výskytu události, dokud neodeberete delegáta.The event handler is called whenever the event occurs, unless you remove the delegate.

Metody rozšíření

GetMethodInfo(Delegate)

Získává objekt, který představuje metodu reprezentovanou zadaným delegátem.Gets an object that represents the method represented by the specified delegate.

Platí pro