DownloadDataCompletedEventHandler DownloadDataCompletedEventHandler DownloadDataCompletedEventHandler DownloadDataCompletedEventHandler Delegate

Definizione

Rappresenta il metodo che gestirà l'evento DownloadDataCompleted di un oggetto WebClient.Represents the method that will handle the DownloadDataCompleted event of a WebClient.

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

Parametri

sender
Object Object Object Object

Origine dell'evento.The source of the event.

Ereditarietà
DownloadDataCompletedEventHandlerDownloadDataCompletedEventHandlerDownloadDataCompletedEventHandlerDownloadDataCompletedEventHandler

Esempi

Nell'esempio di codice riportato di seguito viene illustrato il download di una risorsa specificata dall'utente.The following code example demonstrates downloading a user-specified resource.

// Sample call : DownLoadDataInBackground ("http://www.contoso.com/GameScores.html");
void DownloadDataInBackground( String^ address )
{
   System::Threading::AutoResetEvent^ waiter = gcnew System::Threading::AutoResetEvent( false );
   WebClient^ client = gcnew WebClient;
   Uri ^uri = gcnew Uri(address);
   
   // Specify that the DownloadDataCallback method gets called
   // when the download completes.
   client->DownloadDataCompleted += gcnew DownloadDataCompletedEventHandler( DownloadDataCallback );
   client->DownloadDataAsync( uri, waiter );
   
   // Block the main application thread. Real applications
   // can perform other tasks while waiting for the download to complete.
   waiter->WaitOne();
}


// Sample call : DownLoadDataInBackground ("http://www.contoso.com/GameScores.html");
public static void DownloadDataInBackground (string address)
{
    System.Threading.AutoResetEvent waiter = new System.Threading.AutoResetEvent (false);
    WebClient client = new WebClient ();
    Uri uri = new Uri(address);

    // Specify that the DownloadDataCallback method gets called
    // when the download completes.
    client.DownloadDataCompleted += new DownloadDataCompletedEventHandler (DownloadDataCallback);
    client.DownloadDataAsync (uri, waiter);

    // Block the main application thread. Real applications
    // can perform other tasks while waiting for the download to complete.
    waiter.WaitOne ();
}

'  Sample call : DownLoadDataInBackground ("http:' www.contoso.com/GameScores.html")
Public Shared Sub DownloadDataInBackground(ByVal address As String)

	Dim waiter As System.Threading.AutoResetEvent = New System.Threading.AutoResetEvent(False)
	Dim client As WebClient = New WebClient()

	'  Specify that the DownloadDataCallback method gets called
	'  when the download completes.
	AddHandler client.DownloadDataCompleted, AddressOf DownloadDataCallback
                      Dim uri as Uri = New Uri(address)
	client.DownloadDataAsync(uri, waiter)

	'  Block the main application thread. Real applications
	'  can perform other tasks while waiting for the download to complete.
	waiter.WaitOne()
End Sub

Il metodo seguente viene chiamato al termine del download.The following method is called when the download completes.

void DownloadDataCallback( Object^ /*sender*/, DownloadDataCompletedEventArgs^ e )
{
   System::Threading::AutoResetEvent^ waiter = dynamic_cast<System::Threading::AutoResetEvent^>(e->UserState);
   try
   {
      
      // If the request was not canceled and did not throw
      // an exception, display the resource.
      if (  !e->Cancelled && e->Error == nullptr )
      {
         array<Byte>^data = dynamic_cast<array<Byte>^>(e->Result);
         String^ textData = System::Text::Encoding::UTF8->GetString( data );
         Console::WriteLine( textData );
      }
   }
   finally
   {
      
      // Let the main application thread resume.
      waiter->Set();
   }

}


private static void DownloadDataCallback (Object sender, DownloadDataCompletedEventArgs e)
{
    System.Threading.AutoResetEvent waiter = (System.Threading.AutoResetEvent)e.UserState;

    try
    {
        // If the request was not canceled and did not throw
        // an exception, display the resource.
        if (!e.Cancelled && e.Error == null)
        {
            byte[] data = (byte[])e.Result;
            string textData = System.Text.Encoding.UTF8.GetString (data);

            Console.WriteLine (textData);
        }
    }
    finally
    {
        // Let the main application thread resume.
        waiter.Set ();
    }
}

Private Shared Sub DownloadDataCallback(ByVal sender As Object, ByVal e As DownloadDataCompletedEventArgs)

    Dim waiter As System.Threading.AutoResetEvent = CType(e.UserState, System.Threading.AutoResetEvent)

    Try

        '  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 data() As Byte = CType(e.Result, Byte())
            Dim textData As String = System.Text.Encoding.UTF8.GetString(data)

            Console.WriteLine(textData)
        End If
    Finally

        '  Let the main application thread resume.
        waiter.Set()
    End Try
End Sub

Commenti

Quando si crea un delegato DownloadDataCompletedEventHandler, si identifica il metodo che gestirà l'evento.When you create a DownloadDataCompletedEventHandler delegate, you identify the method that will handle the event. Per associare l'evento al gestore eventi in uso, aggiungere all'evento un'istanza del delegato.To associate the event with your event handler, add an instance of the delegate to the event. Il gestore eventi viene chiamato ogni volta che si verifica l'evento, a meno che non venga rimosso il delegato.The event handler is called whenever the event occurs, unless you remove the delegate. Per ulteriori informazioni sui delegati del gestore eventi, vedere gestione e generazione di eventi.For more information about event handler delegates, see Handling and Raising Events.

Metodi di estensione

GetMethodInfo(Delegate) GetMethodInfo(Delegate) GetMethodInfo(Delegate) GetMethodInfo(Delegate)

Ottiene un oggetto che rappresenta il metodo rappresentato dal delegato specificato.Gets an object that represents the method represented by the specified delegate.

Si applica a