WebClient.DownloadStringCompleted Ereignis

Definition

Tritt ein, wenn ein asynchroner Ressourcendownload abgeschlossen wird.

public:
 event System::Net::DownloadStringCompletedEventHandler ^ DownloadStringCompleted;
public event System.Net.DownloadStringCompletedEventHandler? DownloadStringCompleted;
public event System.Net.DownloadStringCompletedEventHandler DownloadStringCompleted;
member this.DownloadStringCompleted : System.Net.DownloadStringCompletedEventHandler 
Public Custom Event DownloadStringCompleted As DownloadStringCompletedEventHandler 
Public Event DownloadStringCompleted As DownloadStringCompletedEventHandler 

Ereignistyp

Beispiele

Im folgenden Codebeispiel wird das Festlegen eines Ereignishandlers für dieses Ereignis veranschaulicht.

// Sample call : DownloadStringInBackground2 ("http://www.contoso.com/GameScores.html");
void DownloadStringInBackground2( String^ address )
{
   WebClient^ client = gcnew WebClient;
   Uri ^uri = gcnew Uri(address);

   // Specify that the DownloadStringCallback2 method gets called
   // when the download completes.
   client->DownloadStringCompleted += gcnew DownloadStringCompletedEventHandler( DownloadStringCallback2 );
   client->DownloadStringAsync( uri );
}
// Sample call : DownloadStringInBackground2 ("http://www.contoso.com/GameScores.html");
public static void DownloadStringInBackground2(string address)
{
    WebClient client = new WebClient();
    Uri uri = new Uri(address);

    // Specify that the DownloadStringCallback2 method gets called
    // when the download completes.
    client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(DownloadStringCallback2);
    client.DownloadStringAsync(uri);
}
'  Sample call : DownloadStringInBackground2 ("http:' www.contoso.com/GameScores.html")
Public Shared Sub DownloadStringInBackground2(ByVal address As String)

    Dim client As WebClient = New WebClient()

    '  Specify that the DownloadStringCallback2 method gets called
    '  when the download completes.
    AddHandler client.DownloadStringCompleted, AddressOf DownloadStringCallback2
                Dim uri as Uri = New Uri(address)
    client.DownloadStringAsync(uri)
End Sub

Das folgende Codebeispiel zeigt eine Implementierung eines Handlers für dieses Ereignis.

void DownloadStringCallback2( Object^ /*sender*/, DownloadStringCompletedEventArgs^ e )
{

   // If the request was not canceled and did not throw
   // an exception, display the resource.
   if (  !e->Cancelled && e->Error == nullptr )
   {
      String^ textString = dynamic_cast<String^>(e->Result);
      Console::WriteLine( textString );
   }
}
private static void DownloadStringCallback2(Object sender, DownloadStringCompletedEventArgs e)
{
    // If the request was not canceled and did not throw
    // an exception, display the resource.
    if (!e.Cancelled && e.Error == null)
    {
        string textString = (string)e.Result;

        Console.WriteLine(textString);
    }
}
Private Shared Sub DownloadStringCallback2(ByVal sender As Object, ByVal e As DownloadStringCompletedEventArgs)

    '  If the request was not canceled and did not throw
    '  an exception, display the resource.
    If e.Cancelled = False AndAlso e.Error Is Nothing Then

        Dim textString As String = CStr(e.Result)
        Console.WriteLine(textString)
    End If
End Sub

Hinweise

Dieses Ereignis wird jedes Mal ausgelöst, wenn ein asynchroner Vorgang zum Herunterladen einer Ressource ausgelöst wird, wenn eine Zeichenfolge abgeschlossen wird. Diese Vorgänge werden durch Aufrufen der DownloadStringAsync Methoden gestartet.

Der DownloadStringCompletedEventHandler ist der Delegat für dieses Ereignis. Die DownloadStringCompletedEventArgs -Klasse stellt den Ereignishandler mit Ereignisdaten bereit.

Weitere Informationen zum Behandeln von Ereignissen finden Sie unter behandeln und Auslösen von Ereignissen.

Gilt für: