在VC中使用backgroundworker++ 2010年

发布于 2024-10-16 03:37:29 字数 789 浏览 5 评论 0原文

我想制作一个通过备份文件进行压缩的程序,但是在压缩文件时该程序挂起。我尝试使用backgroundworker来执行工作。

这是我到目前为止的代码:

private: System::Void threadTest_Click(System::Object^  sender, System::EventArgs^  e) {
         debug->Text = L"Zipping...";
         zip->RunWorkerAsync();
         if(zip->IsBusy)
         {
            bkProgress->Increment(20);
            Application::DoEvents();
         }
         //zip->OnDoWork();
     }

这是我的后台工作程序的 DoWork 代码:

private: System::Void zip_DoWork(System::Object^  sender, System::ComponentModel::DoWorkEventArgs^  e) {
            //zipping file code here
            refreshAll();
     }

我发现 refreshAll() 不会运行,而且我无法判断线程何时运行停了,我想我在这里做错了什么,你能给我一些提示吗,我对 CLR 真的很陌生。谢谢

I want to make a program that zip by backup files, but the program hangs when zipping files. I tried to use backgroundworker to execute the work.

this is my code so far:

private: System::Void threadTest_Click(System::Object^  sender, System::EventArgs^  e) {
         debug->Text = L"Zipping...";
         zip->RunWorkerAsync();
         if(zip->IsBusy)
         {
            bkProgress->Increment(20);
            Application::DoEvents();
         }
         //zip->OnDoWork();
     }

This is DoWork code for my backgroundworker:

private: System::Void zip_DoWork(System::Object^  sender, System::ComponentModel::DoWorkEventArgs^  e) {
            //zipping file code here
            refreshAll();
     }

I found out that refreshAll()won't run and I can't tell when the thread is stopped, I think I did something wrong here, can you give me some tips, I am really new to CLR. Thanks

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

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

发布评论

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

评论(2

就此别过 2024-10-23 03:37:29

如果 RefreshAll 对 GUI 进行更改,则必须注意以下规则:

切勿从创建 Gui 的主线程以外的其他线程更改 GUI。

在你的情况下,你必须使用 Form::Invoke 方法来切换到主线程

this->Invoke(gcnew RefreshAllDelegate(), new Object[] { message });

,当然你必须创建一个委托 RefreshAllDelegate。

if RefreshAll make changes to GUI, you have to take care to the following rule:

Never change the GUI from another thread than the main thread where the Gui was created.

In you case you have to use Form::Invoke method to switch to the main thread like that

this->Invoke(gcnew RefreshAllDelegate(), new Object[] { message });

And of course you have to create a delegate RefreshAllDelegate.

只有一腔孤勇 2024-10-23 03:37:29

删除 RunWorkerAsync() 之后的所有代码,只有当用户在压缩过程中关闭表单时,它才会使您的应用程序崩溃。在 DoWork() 方法中,使用 bgw 的 ReportProgress() 方法来报告进度。您可以为更新 UI 的事件编写事件处理程序。将refreshAll() 移至bgw 的RunWorkerCompleted 事件处理程序中。

请务必查看 MSDN Library 文章中有关 BackgroundWorker 的示例。 BGW 的设计初衷是让 UI 的多线程变得简单,但如果你使用错误,那么你只会承受很多痛苦。在使其在您自己的代码中运行之前,请先练习示例。

Delete all the code after RunWorkerAsync(), it will only make your app crash when the user closes the form while zipping is in progress. In the DoWork() method, use the bgw's ReportProgress() method to report progress. You can write an event handler for the event to update the UI. Move refreshAll() into the bgw's RunWorkerCompleted event handler.

Do review the examples giving the MSDN Library article for BackgroundWorker. BGW was designed to make multi-threading with the UI easy but if you are using it wrong then you'll just buy a lot of grief. Practice the examples first before making it work in your own code.

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