如何在此上下文中使用 WebClient.DownloadDataAsync() 方法?

发布于 2024-08-07 22:47:54 字数 1132 浏览 2 评论 0原文

我的计划是让用户在我的程序中写下电影标题,我的程序将异步提取适当的信息,这样 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

拔了角的鹿 2024-08-14 22:47:54

有一个较新的 DownloadDataTaskAsync 方法可让您等待结果。到目前为止,它更容易阅读并且更容易连接。我会用那个...

var client = new WebClient();

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

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

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);
南街九尾狐 2024-08-14 22:47:54

您需要处理 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 包含与错误条件等相关的其他信息 - 也请检查这些信息。

另请注意,您将在不同的线程上进入 DownloadDataCompleted;如果您处于 UI(winform、wpf 等)中,则需要在更新 UI 之前进入 UI 线程。在 winforms 中,使用 this.Invoke。对于 WPF,请查看Dispatcher

You need to handle the DownloadDataCompleted event:

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");
}

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, use this.Invoke. For WPF, look at the Dispatcher.

缪败 2024-08-14 22:47:54
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);
}
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);
}
倾`听者〃 2024-08-14 22:47:54

如果有人在 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.

耳根太软 2024-08-14 22:47:54
ThreadPool.QueueUserWorkItem(state => WebClientX.DownloadDataAsync(sitesearchURL));

http://workblog.pilin.name/2009/02/system.html

ThreadPool.QueueUserWorkItem(state => WebClientX.DownloadDataAsync(sitesearchURL));

http://workblog.pilin.name/2009/02/system.html

韶华倾负 2024-08-14 22:47:54

//使用ManualResetEvent类

static ManualResetEvent evnts = new ManualResetEvent(false);
static void Main(string[] args)
{
    byte[] data = null;
    WebClient client = new WebClient();
    client.DownloadDataCompleted += 
        delegate(object sender, DownloadDataCompletedEventArgs e)
        {
             data = e.Result;
             evnts.Set();
        };
    Console.WriteLine("starting...");
    evnts.Reset();
    client.DownloadDataAsync(new Uri("http://stackoverflow.com/questions/"));
    evnts.WaitOne(); // wait to download complete

    Console.WriteLine("done. {0} bytes received;", data.Length);
}

//using ManualResetEvent class

static ManualResetEvent evnts = new ManualResetEvent(false);
static void Main(string[] args)
{
    byte[] data = null;
    WebClient client = new WebClient();
    client.DownloadDataCompleted += 
        delegate(object sender, DownloadDataCompletedEventArgs e)
        {
             data = e.Result;
             evnts.Set();
        };
    Console.WriteLine("starting...");
    evnts.Reset();
    client.DownloadDataAsync(new Uri("http://stackoverflow.com/questions/"));
    evnts.WaitOne(); // wait to download complete

    Console.WriteLine("done. {0} bytes received;", data.Length);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文