如何使用在此范围内WebClient.DownloadDataAsync()方法? [英] How to use the WebClient.DownloadDataAsync() method in this context?
问题描述
我的计划是有一个用户写下电影标题在我的计划,我的程序将异步拉此时,相应的信息,以便用户界面不会冻结。
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.
这里的code:
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.
推荐答案
您需要处理 DownloadDataCompleted
事件:
static void Main()
{
string url = "http://google.com";
WebClient client = new WebClient();
client.DownloadDataCompleted += DownloadDataCompleted;
client.DownloadDataAsync(new Uri(url));
Console.ReadLine();
}
static void DownloadDataCompleted(object sender,
DownloadDataCompletedEventArgs e)
{
byte[] raw = e.Result;
Console.WriteLine(raw.Length + " bytes received");
}
该ARGS包含有关错误情况等信息的其他位 - 检查那些过于
The args contains other bits of information relating to error conditions etc - check those too.
另外请注意,你会进入 DownloadDataCompleted
在不同的线程;如果你是在UI(WinForm的,WPF等),你将需要更新UI之前到UI线程。从的WinForms,使用 this.Invoke
。对于WPF,看调度
。
Also note that you'll be coming into DownloadDataCompleted
on a different thread; if you are in a UI (winform, wpf, etc) you'll need to get to the UI thread before updating the UI. From winforms, use this.Invoke
. For WPF, look at the Dispatcher
.
这篇关于如何使用在此范围内WebClient.DownloadDataAsync()方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!