Questa documentazione è stata archiviata e non viene gestita.

Classe DownloadDataCompletedEventArgs

Aggiornamento: novembre 2007

Fornisce i dati per l'evento DownloadDataCompleted.

Spazio dei nomi:  System.Net
Assembly:  System (in System.dll)

public class DownloadDataCompletedEventArgs : AsyncCompletedEventArgs
public class DownloadDataCompletedEventArgs extends AsyncCompletedEventArgs
public class DownloadDataCompletedEventArgs extends AsyncCompletedEventArgs

Le istanze di questa classe vengono passate al delegato DownloadDataCompletedEventHandler.

Nel codice di esempio riportato di seguito viene illustrato il download di una risorsa specificata dall'utente.

// 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 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.add_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();
} //DownloadDataInBackground


Il metodo riportato di seguito viene chiamato al termine del download.

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 static void DownloadDataCallback(Object sender, 
    DownloadDataCompletedEventArgs e)
{
    System.Threading.AutoResetEvent waiter = 
        (System.Threading.AutoResetEvent)(e.get_UserState());

    try {
        // If the request was not canceled and did not throw
        // an exception, display the resource.
        if (!(e.get_Cancelled()) && e.get_Error() == null) {
            ubyte data[] = (ubyte[])(e.get_Result());
            String textData = System.Text.Encoding.get_UTF8().GetString(data);
            Console.WriteLine(textData);
        }
    }
    finally {
        // Let the main application thread resume.
        waiter.Set();
    }
} //DownloadDataCallback


Qualsiasi membro static (Shared in Visual Basic) pubblico di questo tipo è thread-safe. I membri di istanza non sono garantiti come thread-safe.

Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition , Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

.NET Framework e .NET Compact Framework non supportano tutte le versioni di ciascuna piattaforma. Per un elenco delle versioni supportate, vedere Requisiti di sistema di .NET Framework.

.NET Framework

Supportato in: 3.5, 3.0, 2.0
Mostra: