如何在此上下文中使用 WebClient.DownloadDataAsync() 方法?
我的计划是让用户在我的程序中写下电影标题,我的程序将异步提取适当的信息,这样 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 的清晰示例,以便我可以学习如何使用它?
谢谢你,你总是一个很棒的资源。
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.
Here's the 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;
}
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?
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
有一个较新的 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...
您需要处理
DownloadDataCompleted
事件:args 包含与错误条件等相关的其他信息 - 也请检查这些信息。
另请注意,您将在不同的线程上进入
DownloadDataCompleted
;如果您处于 UI(winform、wpf 等)中,则需要在更新 UI 之前进入 UI 线程。在 winforms 中,使用this.Invoke
。对于 WPF,请查看Dispatcher
。You need to handle the
DownloadDataCompleted
event:The args contains other bits of information relating to error conditions etc - check those too.
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, usethis.Invoke
. For WPF, look at theDispatcher
.如果有人在 Web 应用程序或网站中使用上述内容,请在 aspx 文件的页面指令声明中设置 Async = "true"。
If anyone using above in web application or websites please set Async = "true" in the page directive declaration in aspx file.
http://workblog.pilin.name/2009/02/system.html
http://workblog.pilin.name/2009/02/system.html
//使用ManualResetEvent类
//using ManualResetEvent class