如何在工作线程(非UI线程)中创建模态对话框? [英] How do I create Modal dialog in worker thread(Non-UI thread)?

查看:777
本文介绍了如何在工作线程(非UI线程)中创建模态对话框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个示例MFC应用程序,其中有两个线程:
-Main线程(UI线程)
-Worker线程(非UI线程)

I have written a sample MFC application in which there are two threads: -Main thread ( UI thread) -Worker thread ( non-UI thread)

我有一个特定的要求在非UI(工作线程)中创建一个 Modal 对话框。
当我创建 CDialog 对象并调用 DoModal 时,它工作。对话框被创建并作为应用程序的Modal。 (Win XP SP2机)但这不工作在Windows 2003服务器机器。
2003服务器的行为是,Modal对话框位于应用程序的主窗口之后,只有当我单击主窗口时,对话框才会显示在前面。它不作为我的应用程序的模态对话框。

I have a specific requirement to create a Modal dialog in Non-UI ( worker thread). When I create the CDialog object and call DoModal on the same, it works. The dialog gets created and acts as Modal to the application. ( Win XP SP2 machine) But this does not work in Windows 2003 server machine. The behavior in 2003 server is that, the Modal Dialog goes behind the application main Window and dialog will be brought to front only when I click on Main Window. It is not acting as Modal dialog to my application.

什么可能是问题 - 任何想法?

What could be the problem -- any ideas?

如果在非UI线程中创建UI控件是问题,那么有任何Win32 API,将允许我链接我的工作线程到主UI线程,使 DoModal 发生在主线程中。我尝试 AttachThreadInput 但它不工作。

If creating UI controls in non-UI thread is the issue then is there any Win32 API which will allow me to link my worker thread to Main UI thread such that DoModal happens in Main thread. I tried AttachThreadInput but it is not working.

推荐答案

没有可靠的方法来跨多个线程传播GUI模式。每个窗口由通过 HWND 引用的对象表示,该对象又具有线程亲和性。这是16位Windows的剩余部分,没有多线程。因此, HWND s不受保护以免并发访问。 The Old New Thing 有一个关于线程亲和力的用户界面对象(部分 1 2 3 附录)。

There is no reliable way to spread GUI modality across multiple threads. Every window is represented by an object referenced through a HWND which in turn has thread affinity. This is a left-over from the 16-bit days of Windows, where there was no multi threading. Consequently the HWNDs are not protected against concurrent access. The Old New Thing has an excellent series on "Thread affinity of user interface objects" (Part 1 2 3 Addendum).

通过首先启用对话框窗口,然后禁用其父窗口,实现模态。第一步是安全的,而第二步尝试从不是窗口拥有线程的线程禁用窗口。由于en /禁用窗口修改了通过 HWND 引用的对象,它代表一个竞争条件。

Modality is implemented by first enabling the dialog window and then disabling its parent. The first step is safe while the second attempts to disable a window from a thread which is not the window's owning thread. Since en-/disabling windows modifies the object referenced through the HWND it represents a race condition.

是将你的GUI限制在一个线程,并从你的工作线程沟通到GUI线程,让它执行用户在工作线程的交互。要完成此操作的最简单方法是调用 SendMessage 从工作线程阻塞直到GUI线程的消息处理程序返回。如果工作线程在对话框显示时继续运行,可以使用 PostMessage ,然后使用 PostThreadMessage 或发送同步对象,如事件对象

The suggested solution is to confine your GUI to a single thread and communicate from your worker thread to the GUI thread to have it perform user interaction on the worker thread's behalf. The easiest way to accomplish this is to call SendMessage from the worker thread to block until the GUI thread's message handler returns. If the worker thread should continue to run while the dialog is displayed you could use PostMessage instead and communicate back to the worker thread using PostThreadMessage or signaling a synchronization object like an Event Object.

这篇关于如何在工作线程(非UI线程)中创建模态对话框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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