这个异步下载可以吗? WebClient.DownloadDataAsyn() 问题

发布于 2024-08-08 14:02:43 字数 1853 浏览 8 评论 0 原文

我有一个名为 SiteAsyncDownload.cs 的类,

代码如下:

public class SiteAsyncDownloader
{
    WebClient Client = new WebClient();
    string SiteSource = null;

    /// <summary>
    /// Download asynchronously the source code of any site in string format.
    /// </summary>
    /// <param name="URL">Site URL to download.</param>
    /// <returns>Website source code - string.</returns>
    public string GetSite(string URL)
    {
        Uri Site = new Uri(URL);

        Client.DownloadDataAsync(Site);
        Client.DownloadDataCompleted += new DownloadDataCompletedEventHandler(FinishedDownloading);

        //Don't know if this would work. Consult with StackOverflow.
        while (true)
        {
            if (SiteSource != null)
            {
                return SiteSource;
            }
        }            
    }

    void FinishedDownloading(object sender, DownloadDataCompletedEventArgs e)
    {
        SiteSource = Encoding.ASCII.GetString(e.Result);
        throw new NotImplementedException();
    }
}

我不是 100% 确信它能按我希望的方式工作。我希望该类异步下载所需的任何内容,并在下载完成后返回字符串。这是正确的方法吗?

例如,下面是我打算如何使用它的示例:

SiteAsyncDownloader Downloader = new SiteAsyncDownloader();

    /// <summary>
    /// Search for a movie using only a name.
    /// </summary>
    /// <param name="MovieName">Movie name.</param>        
    public void SearchMovie(string MovieName)
    {
        string SearchURL = FormatSearch(MovieName);

        string SearchSource = Downloader.GetSite(SearchURL);

        string MovieURL = FindMovieURL(SearchSource);
        string MovieSource = Downloader.GetSite(MovieURL);

        FindInformation(MovieSource);
    }

在 SearchMovie() 方法的第二行代码中,我的程序会因为异步下载而崩溃吗?我怎样才能考虑到这一点并使其正常工作?

I have this class called SiteAsyncDownload.cs

Here's the code:

public class SiteAsyncDownloader
{
    WebClient Client = new WebClient();
    string SiteSource = null;

    /// <summary>
    /// Download asynchronously the source code of any site in string format.
    /// </summary>
    /// <param name="URL">Site URL to download.</param>
    /// <returns>Website source code - string.</returns>
    public string GetSite(string URL)
    {
        Uri Site = new Uri(URL);

        Client.DownloadDataAsync(Site);
        Client.DownloadDataCompleted += new DownloadDataCompletedEventHandler(FinishedDownloading);

        //Don't know if this would work. Consult with StackOverflow.
        while (true)
        {
            if (SiteSource != null)
            {
                return SiteSource;
            }
        }            
    }

    void FinishedDownloading(object sender, DownloadDataCompletedEventArgs e)
    {
        SiteSource = Encoding.ASCII.GetString(e.Result);
        throw new NotImplementedException();
    }
}

I'm not 100% if this would work as I want it to. I want the class to download whatever it needs to asynchronously and when done downloading return the string. Is this the right approach?

For instance, here's an example of how I intend to use it:

SiteAsyncDownloader Downloader = new SiteAsyncDownloader();

    /// <summary>
    /// Search for a movie using only a name.
    /// </summary>
    /// <param name="MovieName">Movie name.</param>        
    public void SearchMovie(string MovieName)
    {
        string SearchURL = FormatSearch(MovieName);

        string SearchSource = Downloader.GetSite(SearchURL);

        string MovieURL = FindMovieURL(SearchSource);
        string MovieSource = Downloader.GetSite(MovieURL);

        FindInformation(MovieSource);
    }

In the second line of code in the SearchMovie() method, will my program crash because it's of the async download? How can I take this into account and have it work properly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

爱给你人给你 2024-08-15 14:02:43

您在这里尝试执行的操作:

while (true)

只会启动 CPU 直至操作完成,从而打破了异步操作的初衷。您需要使用事件模式回调模式

What you're trying to do here:

while (true)

Will just spin up the CPU until the operation is done, defeating the point of it being asynchronous in the first place. You would need to use the event pattern or the callback pattern.

oО清风挽发oО 2024-08-15 14:02:43

虽然它可能会起作用,但这是一个坏主意。如果您要立即阻塞直到结果出来,那么使用异步 API 就没有意义。如果在获得数据之前您确实无法在该线程中执行任何其他工作,并且您只有一个请求,则只需使用同步 API。此外,您阻止的方式只是紧密循环,这将导致您的处理器无缘无故地旋转。

在稍微不同的问题上,如果我是你,我不会使用 Encoding.ASCII - 你应该尊重服务器发送的任何编码。使用 DownloadStringAsync 是最简单的方法。

While it will probably work, it's a bad idea. There's no point in using asynchronous APIs if you're immediately going to block until the results have come through. If you really can't do any more work in that thread until you've got the data, and you've only got one request, just use a synchronous API. Further, the way you're blocking is to just tight loop, which will cause your processor to spin for no reason.

On a slightly different matter, I wouldn't use Encoding.ASCII if I were you - you should honour whatever encoding the server sends. Using DownloadStringAsync is the easiest way of doing that.

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