How to create a MFC dialog with a progress bar in a separate thread?(如何在单独的线程中创建带有进度条的 MFC 对话框?)
问题描述
我的应用程序可能需要一段时间才能连接到数据库.这种连接是通过单个库函数调用建立的,即我不能将进度更新放在那里并进行回调或类似的东西.
My application may take a while to connect to a database. This connection is made with a single library function call, i.e. I cannot put progress updates in there and make callbacks or something similar.
我的想法是在连接到数据库之前在单独的线程中创建一个带有进度条的对话框.此对话框将使用 CProgressCtrl::StepIt()
不断更改进度状态,以便用户看到正在发生的事情.
在设置好对话框并完成它的事情之后,我想从主线程调用 DB 连接函数.连接功能完成后,我想停止进度条线程.
My idea was to create a dialog with a progress bar in a separate thread before connecting to the DB. This dialog will continually change the progress status with CProgressCtrl::StepIt()
so the user sees something happening.
After that dialog is set up and doing its thing I want to call the DB connection function from the main thread.
After the connection function completed, I want to stop the progress bar thread.
让我画一幅画:
CMyApp:: ProgressThread
InitInstance() .
| .
| .
+-Create Dialog-+
| |
| Animate
Connect Progress
to Bar
DB |
| |
+-Destroy Dlg---+
| .
| .
这可能吗?如果是,怎么做?
Is that possible? If yes, how?
也许整个事情也可以使用计时器.可能会简单得多,但我也无法让它工作.
Maybe the whole thing would work using timers, too. Would probably be much simpler but I couldn't get that to work either.
- 我知道
CProgressCtrl::SetMarquee()
可能完全符合我的需要,但我无法使用它,因为该应用程序不支持 Unicode. - 我可以将 db 连接调用移到一个单独的线程中,但这样看起来对代码进行了很多更改,并且对连接错误进行了额外的处理.
- I am aware of
CProgressCtrl::SetMarquee()
which might do exactly what I need but I can't use it because the application does not have Unicode support. - I could move the db connection call into a separate thread but that way it looks like a lot of changes to the code and extra handling of connection errors.
更新 2
我让它按照 AlexEzh 和 Javier De Pedro 建议的方式工作:将 DB stuf 放入自己的线程中.
最初我担心如何进行错误处理,但实际上它与以前非常相似.
Update 2
I got it working the way AlexEzh and Javier De Pedro suggested: Put the DB stuf into its own thread.
initially I had concerns about how error handling could be done but it's actually quite similar to how it was before.
- 在主线程中,我创建了一个带有连接参数、结果标志和线程运行标志的结构.后者最初设置为
true
. - 我创建了一个线程并将该结构作为参数传递.
- 我在主线程中创建了一个显示进度条的对话框.
- 在主线程中还有一个循环在设置线程运行标志时运行.它调用
CMyDialog::Animate()
调用CProgressCtrl::StepIt()
然后Sleep()
一点. - 线程执行 db-connection 代码并在完成后将运行标志设置为
false
. - 当主线程退出循环时,它可以像以前一样处理错误.
- In the main thread I create a struct with connection parameters, result flag and thread-running-flag. The latter is initially set to
true
. - I create a thread and pass that struct as parameter.
- I create a dialog that displays a progress bar in the main thread.
- Also in the main thread there is a loop that runs while the thread-running-flag is set. It calls
CMyDialog::Animate()
which callsCProgressCtrl::StepIt()
and thenSleep()
s a bit. - The thread executes the db-connection code and sets the running-flag to
false
when done. - When the main thread exits the loop it can handle errors exactly as it did before.
缺点:将鼠标移到窗口上不起作用.它是看不见的.因此不能使用取消按钮或其他交互式对话框元素.不过,我可以忍受.
Disadvantage: Moving the mouse over the window doesn't work. It's invisible. Thus no cancel-button or other interactive dialog elements can be used. I can live with that, however.
由于您喜欢该图表,现在它的外观如下:
Since you liked the diagram, here is how it now looks like:
CMyApp:: WorkerThread
InitInstance() .
| .
| .
Create Dialog .
| .
+-Start Thread--+
| |
| Connect
Animate to
Progress DB
Bar |
| |
+-Thread Ends---+
| .
Destroy Dlg .
| .
推荐答案
将 DB 连接逻辑移动到单独的线程仍然会更安全.使用对话框线程上的 DB,您将能够重绘进度条,但不能重绘对话框中的其他控件.
It would still be safer to move the DB connection logic to the separate thread. With DB on the dialog thread, you will be able to repaint the progress bar but not other controls in the dialog.
这篇关于如何在单独的线程中创建带有进度条的 MFC 对话框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在单独的线程中创建带有进度条的 MFC 对话框?
基础教程推荐
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 从 std::cin 读取密码 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 使用从字符串中提取的参数调用函数 2022-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- Windows Media Foundation 录制音频 2021-01-01