【问题标题】:DownloadStringAsync inside a Backgroundworker vb.net在 Backgroundworker vb.net 中下载StringAsync
【发布时间】:2013-10-12 02:37:19
【问题描述】:

我很困惑如何做到这一点。

我有一堆后台工作人员,他们都需要使用 Webclients downloadstringasync 方法。

我的问题是如何从下载的字符串中返回数据(一旦下载字符串完成事件发生),返回到启动下载字符串的后台工作人员?

我希望这是有道理的。任何建议将不胜感激。

【问题讨论】:

  • 我这样做的原因是我可以通过使用 cancelasync 使其超时,因为我正在使用喜欢导致正常下载字符串挂起的代理。

标签: c# vb.net asynchronous backgroundworker webclient


【解决方案1】:

在工作线程中使用异步版本毫无意义。由于BackgroundWorker,代码已经异步运行。

所以只需使用 DownloadString() 代替,问题就解决了。

【讨论】:

  • -1 asych 和 concurrent 略有不同。使用后台工作者会消耗一个线程。异步回调不消耗线程。在 x64 机器上,线程使用 8mb 内存。以及其他资源。
  • @HansPassant 这是真的。但是您说在工作线程中使用异步版本没有意义。使用线程池线程不会降低线程的成本。仍然使用线程。如果您有 1000 个请求,则使用带有后台线程的同步 DownloadString() 将在线程池最大时阻塞,或者将在线程池上创建新线程。至于使用 DownloadStringAsync() 的后台工作线程,它将请求创建移到工作线程上,这在 UI 密集型应用程序中可能很重要。
  • 他已经在使用 BGW。他可以用它做两件事,调用 DownloadString() 或阻止它直到 DownloadStringAsync 完成。这些选择中只有一个是有意义的。
【解决方案2】:

如果您使用WebClient.DownloadStringAsync,它将在不同的线程上运行。您需要添加事件处理程序才能在事件发生时得到通知。

例如:

此外,这里还有一些从 Google Async 下载图像的示例代码。

using System;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Threading;

namespace WebDownload
{
    class Program
    {
        static AutoResetEvent autoEvent = new AutoResetEvent(false);

        static void Main(string[] args)
        {
            WebClient client = new WebClient();
            client.DownloadFileCompleted += new AsyncCompletedEventHandler(DownloadCompleted);

            string fileName = Path.GetTempFileName();
            client.DownloadFileAsync(new Uri("https://www.google.com/images/srpr/logo11w.png"), fileName, fileName);

            // Wait for work method to signal. 
            if (autoEvent.WaitOne(20000))
                Console.WriteLine("Image download completed.");
            else
            {
                Console.WriteLine("Timed out waiting for image to download.");
                client.CancelAsync();
            }
        }

        private static void DownloadCompleted(object sender, AsyncCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                Console.WriteLine(e.Error.Message);
                if (e.Error.InnerException != null)
                    Console.WriteLine(e.Error.InnerException.Message);
            }
            else
            {
                // We have a file - do something with it
                WebClient client = (WebClient)sender;

                // display the response header so we can learn
                Console.WriteLine("Response Header...");
                foreach(string k in client.ResponseHeaders.AllKeys)
                    Console.WriteLine("   {0}: {1}", k, client.ResponseHeaders[k]);
                Console.WriteLine(string.Empty);

                // since we know it's a png, let rename it
                FileInfo temp = new FileInfo((string)e.UserState);
                string pngFileName = Path.Combine(Path.GetTempPath(), "DesktopPhoto.png");
                if (File.Exists(pngFileName))
                    File.Delete(pngFileName);

                File.Move((string)e.UserState, pngFileName);  // move to where ever you want
                Process.Start(pngFileName);     // display the photo for the fun of it
            }

            // Signal that work is finished.
            autoEvent.Set();
        }


    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-17
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 2011-08-06
    相关资源
    最近更新 更多