如何在单独的线程中创建带有进度条的 MFC 对话框? [英] How to create a MFC dialog with a progress bar in a separate thread?

查看:24
本文介绍了如何在单独的线程中创建带有进度条的 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.

  1. 我知道 CProgressCtrl::SetMarquee() 可能完全符合我的需要,但我无法使用它,因为该应用程序不支持 Unicode.
  2. 我可以将 db 连接调用移到一个单独的线程中,但这样看起来对代码进行了很多更改,并且对连接错误进行了额外的处理.
  1. 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.
  2. 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.

  1. 在主线程中,我创建了一个带有连接参数、结果标志和线程运行标志的结构.后者最初设置为 true.
  2. 我创建了一个线程并将该结构作为参数传递.
  3. 我在主线程中创建了一个显示进度条的对话框.
  4. 在主线程中还有一个循环在设置线程运行标志时运行.它调用 CMyDialog::Animate() 调用 CProgressCtrl::StepIt() 然后 Sleep() 一点.
  5. 线程执行 db-connection 代码并在完成后将运行标志设置为 false.
  6. 当主线程退出循环时,它可以像以前一样处理错误.
  1. In the main thread I create a struct with connection parameters, result flag and thread-running-flag. The latter is initially set to true.
  2. I create a thread and pass that struct as parameter.
  3. I create a dialog that displays a progress bar in the main thread.
  4. Also in the main thread there is a loop that runs while the thread-running-flag is set. It calls CMyDialog::Animate() which calls CProgressCtrl::StepIt() and then Sleep()s a bit.
  5. The thread executes the db-connection code and sets the running-flag to false when done.
  6. 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 对话框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆