How to use the WebClient.DownloadDataAsync() method in this context?

前端 未结 6 1671
北荒
北荒 2020-12-01 18:01

My plan is to have a user write down a movie title in my program and my program will pull the appropiate information asynchronously so the UI doesn\'t freeze up.

Her

6条回答
  •  余生分开走
    2020-12-01 18:48

    static void Main(string[] args)
    {
        byte[] data = null;
        WebClient client = new WebClient();
        client.DownloadDataCompleted += 
           delegate(object sender, DownloadDataCompletedEventArgs e)
           {
                data = e.Result;
           };
        Console.WriteLine("starting...");
        client.DownloadDataAsync(new Uri("http://stackoverflow.com/questions/"));
        while (client.IsBusy)
        {
             Console.WriteLine("\twaiting...");
             Thread.Sleep(100);
        }
        Console.WriteLine("done. {0} bytes received;", data.Length);
    }
    

提交回复
热议问题