如何使用WebClient而不阻塞UI?

发布于 2024-11-09 06:00:04 字数 993 浏览 0 评论 0原文

有人可以向我指出教程或提供一些示例代码来调用 System.Net.WebClient().DownloadString(url) 方法,而无需在等待结果时冻结 UI 吗?

我认为这需要用线程来完成?是否有一个简单的实现可以使用而不需要太多的开销代码?

谢谢!


已实现 DownloadStringAsync,但 UI 仍然冻结。有什么想法吗?

    public void remoteFetch()
    {
            WebClient client = new WebClient();

            // Specify that the DownloadStringCallback2 method gets called
            // when the download completes.
            client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(remoteFetchCallback);
            client.DownloadStringAsync(new Uri("http://www.google.com"));
    }

    public void remoteFetchCallback(Object sender, DownloadStringCompletedEventArgs e)
    {
        // If the request was not canceled and did not throw
        // an exception, display the resource.
        if (!e.Cancelled && e.Error == null)
        {
            string result = (string)e.Result;

            MessageBox.Show(result);

        }
    }

Can someone point me to a tutorial or provide some sample code to call the System.Net.WebClient().DownloadString(url) method without freezing the UI while waiting for the result?

I assume this would need to be done with a thread? Is there a simple implementation I can use without too much overhead code?

Thanks!


Implemented DownloadStringAsync, but UI is still freezing. Any ideas?

    public void remoteFetch()
    {
            WebClient client = new WebClient();

            // Specify that the DownloadStringCallback2 method gets called
            // when the download completes.
            client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(remoteFetchCallback);
            client.DownloadStringAsync(new Uri("http://www.google.com"));
    }

    public void remoteFetchCallback(Object sender, DownloadStringCompletedEventArgs e)
    {
        // If the request was not canceled and did not throw
        // an exception, display the resource.
        if (!e.Cancelled && e.Error == null)
        {
            string result = (string)e.Result;

            MessageBox.Show(result);

        }
    }

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

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

发布评论

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

评论(2

始终不够爱げ你 2024-11-16 06:00:04

查看 WebClient.DownloadStringAsync() 方法,这可以让您异步发出请求而不阻塞 UI 线程。

var wc = new WebClient();
wc.DownloadStringCompleted += (s, e) => Console.WriteLine(e.Result);
wc.DownloadStringAsync(new Uri("http://example.com/"));

(另外,完成后不要忘记 Dispose() WebClient 对象)

Check out the WebClient.DownloadStringAsync() method, this'll let you make the request asynchronously without blocking the UI thread.

var wc = new WebClient();
wc.DownloadStringCompleted += (s, e) => Console.WriteLine(e.Result);
wc.DownloadStringAsync(new Uri("http://example.com/"));

(Also, don't forget to Dispose() the WebClient object when you're finished)

眼睛会笑 2024-11-16 06:00:04

您可以使用BackgroundWorker 或@Fulstow 所说的DownStringAsynch 方法。

这是有关 Backgorund Worker 的教程:http://www.dotnetperls.com/backgroundworker

You could use a BackgroundWorker or as @Fulstow said the DownStringAsynch method.

Here's a tutorial on Backgorund worker: http://www.dotnetperls.com/backgroundworker

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