在BackgroundWorker线程中下载文件时GUI冻结

发布于 2024-11-08 07:04:02 字数 874 浏览 3 评论 0原文

BackgroundWorker 线程(以 RunWorkerAsync() 启动)中,我想通过 HttpWebRequest 从服务器下载文件。一旦我调用 GetResponse(),从服务器下载文件时 GUI 就会保持冻结状态。这实际上不应该发生,因为 BackgroundWorker 或多或少与带有一些装饰(如进度处理和取消)的线程相同。谁能解释一下为什么会发生这种情况?

这里是代码片段:

            if ((lRequest = (HttpWebRequest)WebRequest.Create(lURL)) != null)
            {
                if ((lResponse = lRequest.GetResponse()) != null)
                {
                    lRemoteStream = lResponse.GetResponseStream();
                    lLocalStream = File.Create(lTempFileName);

                    do
                    {
                        lBytesRead = lRemoteStream.Read(lTemp, 0, lTemp.Length);
                        lLocalStream.Write(lTemp, 0, lBytesRead);
                    } while (lBytesRead > 0);
                }
            }

In the BackgroundWorker thread (started with RunWorkerAsync()) I want to download files from a server via HttpWebRequest. As soon as I call GetResponse() the GUI is remains freezed while the file is downloaded from the server. This actually shouldn't happen as a BackgroundWorker is more or less the same as a thread with some decoration like progress handling and cancellation. Can anybody explain me why this is happening?

Here the code snippet :

            if ((lRequest = (HttpWebRequest)WebRequest.Create(lURL)) != null)
            {
                if ((lResponse = lRequest.GetResponse()) != null)
                {
                    lRemoteStream = lResponse.GetResponseStream();
                    lLocalStream = File.Create(lTempFileName);

                    do
                    {
                        lBytesRead = lRemoteStream.Read(lTemp, 0, lTemp.Length);
                        lLocalStream.Write(lTemp, 0, lBytesRead);
                    } while (lBytesRead > 0);
                }
            }

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

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

发布评论

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

评论(2

影子的影子 2024-11-15 07:04:13

与此同时,我发现了问题,如果我听了你的话,你可能已经回答了我的问题:/

如果你启动BackgroundWorker线程,请小心你不要指向UI主线程。
就我而言,我是这样做的:

    private void BW_FriendInRequest_DoWork(object sender, DoWorkEventArgs e)
    {
        while (true)
        {
            if (this.isLoggedIn)
                listIncomingRequests();

            Thread.Sleep(Config.mMessagesCheckSleep);
        }
    }


    private delegate void listIncomingRequestsDelegate();
    private void listIncomingRequests()
    {
        if (InvokeRequired)
        {
            listIncomingRequestsDelegate d = new listIncomingRequestsDelegate(listIncomingRequests);
            this.Invoke(d, new object[] { });
            return;
        }
       ...
    }

一旦我删除“InvokeRequired”,冻结效果就消失了。

in the meantime i've found the problem and if i had listened to you you had probably answered my question already :/

if you start BackgroundWorker threads be careful you don't point back to the UI main thread.
in my case i did it this way :

    private void BW_FriendInRequest_DoWork(object sender, DoWorkEventArgs e)
    {
        while (true)
        {
            if (this.isLoggedIn)
                listIncomingRequests();

            Thread.Sleep(Config.mMessagesCheckSleep);
        }
    }


    private delegate void listIncomingRequestsDelegate();
    private void listIncomingRequests()
    {
        if (InvokeRequired)
        {
            listIncomingRequestsDelegate d = new listIncomingRequestsDelegate(listIncomingRequests);
            this.Invoke(d, new object[] { });
            return;
        }
       ...
    }

as soon as i removed the "InvokeRequired" the freezing effect had gone.

怪异←思 2024-11-15 07:04:12

需要查看更多代码来提供帮助。

我遇到类似问题的一种方式是,当我非常快速地向 UI 发送大量进度时,这实际上冻结了它。因此,虽然我的主要任务在像您一样的另一个线程上运行,但我对 UI 的更新速度非常快,这对 UI 线程产生了负面影响。也许你在这里做类似的事情?

Need to see more code to offer help.

One way I had a similar issue happen to me was when I was sending a lot of progress to the UI very quickly which in essence froze it. So while my main task was running on another thread like yours, my updates to the UI were extremely fast that it affected the UI thread negatively. Maybe you're doing something similar here?

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