如何在这种情况下使用 WebClient.DownloadDataAsync() 方法? [英] How to use the WebClient.DownloadDataAsync() method in this context?
问题描述
我的计划是让用户在我的程序中写下电影标题,我的程序将异步提取适当的信息,这样 UI 就不会冻结.
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.
代码如下:
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?
My actual problem lies in the WebClientX.DownloadDataAsync() method. I can't use a string URL for it. How can I use that built in function to download the bytes of the site (for later use I will convert this to string, I know how to do this) and without freezing up my GUI?
也许是一个清晰的 DownloadDataAsync 示例,以便我可以学习如何使用它?
Perhaps a clear cut example of the DownloadDataAsync so I can learn how to use it?
谢谢,你总是很棒的资源.
Thanks SO, you're always such a great resource.
推荐答案
有一个较新的 DownloadDataTaskAsync 方法可以让您等待结果.到目前为止,它更易于阅读且更易于连接.我会用那个...
There is a newer DownloadDataTaskAsync method that allows you to await the result. It is simpler to read and easier to wire up by far. I'd use that...
var client = new WebClient();
var data = await client.DownloadDataTaskAsync(new Uri(imageUrl));
await outstream.WriteAsync(data, 0, data.Length);
这篇关于如何在这种情况下使用 WebClient.DownloadDataAsync() 方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!