mercredi 7 octobre 2015

Async Web Client Downloading in C# and .NET 3.5

I am using Windows Professional 64 bit, .NET v3.5, Visual C# 2008 Compiler for following code.

using System;
using System.Net;
using System.Collections.Generic;
using System.Configuration;
using System.ComponentModel;

public class Test {
    public static void Main(){
        string strUrl = "http://ift.tt/1Lkd3AA";
        string strFile = "D:\\nav_logo231.png";
        WebClient objWeb = new WebClient();             
        //objWeb.DownloadFileCompleted  += Test.Hello(); 
        objWeb.DownloadFileCompleted  += new AsyncCompletedEventHandler(objWeb_DownloadFileCompleted);
        //objWeb.DownloadProgressChanged += new DownloadProgressChangedEventHandler(objWeb_DownloadProgressChanged);
        objWeb.DownloadFileAsync(new Uri(strUrl), strFile);
        while (objWeb.IsBusy) { 
        }
        Console.WriteLine("Done");
    }
    public void objWeb_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e) {
        // display completion status.
        if (e.Error != null)
        Console.WriteLine(e.Error.Message);
        else
        Console.WriteLine("Download Completed!!!");
    }
}

// private void objWeb_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e) {
//            // print progress of download.
//            Console.WriteLine(e.BytesReceived + " " + e.ProgressPercentage);
// }

// public void Hello() {
//      Console.WriteLine("Hello");
// }

I tried to make this work, but my limited knowledge puts me in three situations:

error CS0246: The type or namespace name 'AsyncCompletedEventArgs' could not be found (are you missing a using directive or an assembly reference?)


error CS0120: An object reference is required for the non-static field, method, or property 'Test.objWeb_DownloadFileCompleted(object, System.ComponentModel.AsyncCompletedEventArgs)'
downloading.cs(20,14): (Location of symbol related to previous error)

and while calling custom function Hello

downloading.cs(13,36): error CS0120: An object reference is required for the
        non-static field, method, or property 'Test.Hello()'
downloading.cs(20,7): (Location of symbol related to previous error)

While reading documentation on MSDN, I could not find any info about while (objWeb.IsBusy), which if not used gives 0 KB file.
I want it to work with multiple urls at same time. But that's too far until it runs correct for single url.




Aucun commentaire:

Enregistrer un commentaire