我的计划是让用户在程序中写下电影标题,并且程序将异步提取适当的信息,以使UI不会冻结。

这是代码:

public class IMDB
    {
        WebClient WebClientX = new WebClient();
        byte[] Buffer = null;


        public string[] SearchForMovie(string SearchParameter)
        {
            //Format the search parameter so it forms a valid IMDB *SEARCH* url.
            //From within the search website we're going to pull the actual movie
            //link.
            string sitesearchURL = FindURL(SearchParameter);

            //Have a method download asynchronously the ENTIRE source code of the
            //IMDB *search* website.
            Buffer = WebClientX.DownloadDataAsync(sitesearchURL);


            //Pass the IMDB source code to method findInformation().

            //string [] lol = findInformation();

            //????

            //Profit.

            string[] lol = null;
            return lol;
        }

我的实际问题在于WebClientX.DownloadDataAsync()方法。我不能使用字符串URL。如何使用该内置函数下载站点的字节(供以后使用,我会将其转换为字符串,我知道该怎么做),而又不冻结我的GUI?

也许是DownloadDataAsync的明确示例,以便我可以学习如何使用它?

谢谢您,您一直以来都是一个很棒的资源。

最佳答案

有一个更新的DownloadDataTaskAsync方法,使您可以等待结果。到目前为止,它更易于阅读,也更容易接线。我会用那个...

var client = new WebClient();

var data = await client.DownloadDataTaskAsync(new Uri(imageUrl));

await outstream.WriteAsync(data, 0, data.Length);

09-30 14:29
查看更多