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

查看:319
本文介绍了如何在单独的线程中使用进度条创建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. 我可以移动数据库连接调用到一个单独的线程,但是这样看起来像代码的很多变化和连接错误的额外处理。

  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() sa bit。

  5. 线程执行db-connection代码,并将running-flag设置为 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天全站免登陆