COM在多线程程序中的使用 [英] COM Use in Multithreaded Program

查看:70
本文介绍了COM在多线程程序中的使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我在我的C ++ Qt GUI Windows程序中使用COM。 这是一个数据备份程序。 我有一个工作线程来执行文件复制操作,还有一个主GUI线程用于其余操作。 我在worker
线程中使用卷影复制服务和IFileOperation,卷影复制服务用于使卷影复制服务快照从中复制数据,而IFileOperation用于将文件发送到回收站。 我在主GUI线程中使用Task Scheduler API和IShellLink接口,让程序
从用户登录开始,并分别创建程序可执行文件的快捷方式。 目前,我只需要在各自的线程中使用COM对象之前调用CoInitializeEx(NULL,COINIT_APARTMENTTHREADED);
。 对象保留在创建它们的主题中。 完成后我调用CoUninitialize();. 这是正确的COM线程使用吗? 我应该使用CoInitializeEx(NULL,
COINIT_MULTITHREADED);? 我是否冒任何竞争条件或我做得不好? 我是否需要提取消息循环? 还有什么需要知道的吗?

谢谢!

Hi I am using COM in my C++ Qt GUI Windows program.  It is a data backup program.  I have a worker thread to do file copy operations and a main GUI thread for the rest.  I use Volume Shadow Copy Service and IFileOperation in the worker thread, Volume Shadow Copy Service for making volume shadow copy service snapshots to copy data from, and IFileOperation for sending files to the recycle bin.  I use the Task Scheduler API and IShellLink interface in the main GUI thread to have the program start at user logon and create shortcuts to the program executable respectively.  Currently I just call CoInitializeEx(NULL, COINIT_APARTMENTTHREADED); when needed before using COM objects in their respective thread.  The objects stay in the thread they were created in.  When done I call CoUninitialize();.  Is this proper COM threading usage?  Should I be using CoInitializeEx(NULL, COINIT_MULTITHREADED);?  Do I risk any race conditions or am I doing things properly?  Do I need to pump a message loop?  Anything else to know?
Thanks!

推荐答案

Shell接口等因为IFileOperation和IShellLink需要一个单线程的公寓。 所以在我看来,使用COINIT_APARTMENTTHREADED是正确的决定,不应该改变。 并且UI线程通常应该在
STA中。 请参阅
用户界面代码+多线程公寓=死亡




来自
单线程公寓
-

"每个单线程单元必须有一个消息循环来处理来自同一进程中其他进程和公寓的调用。没有对象的单线程公寓(仅限客户端)也需要一个消息循环来调度某些应用程序使用的广播消息
"

"Each single-threaded apartment must have a message loop to handle calls from other processes and apartments within the same process. Single-threaded apartments without objects (client only) also need a message loop to dispatch the broadcast messages that some applications use"


这篇关于COM在多线程程序中的使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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