WTL multithreading, multiple interfaces & libraries

本小妞迷上赌 提交于 2020-01-22 02:44:09

问题


I have a Main Thread that displays an interface, within another thread created from the main thread before the Main interface is shown, I create tow other windows sequentially: I create the first window:

    CWarningDlg warnDlg;
    warnDlg.Create(NULL);
    warnDlg.ShowWindow(SW_SHOW);
    warnDlg.BringWindowToTop();
    CMessageLoop _Loop ;

    if(_MyAppModule.AddMessageLoop(&_Loop))
    {
        nRet = _Loop.Run();
        _MyAppModule.RemoveMessageLoop();
    }
    warnDlg.DestroyWindow();
    if (nRet == SOME_VALUE)
    {
       doSomethingElse();
    }

Do something else has:

 CActionDlg actDlg;
    actDlg.Create(NULL);
    actDlg.ShowWindow(SW_SHOW);
    actDlg.BringWindowToTop();
    CMessageLoop _Loop ;

    if(_MyAppModule.AddMessageLoop(&_Loop))
    {
        CreateAnObject(); //this also launches an object Specific Worker Thread
        nRet = _Loop.Run();
        _MyAppModule.RemoveMessageLoop();
    }

The function CreateAnObject calls some functions from a 'ComplexObject.DLL' that create an complex object which holds the THREAD ID of the thread that called the CREATION function, it gets it with ::GetCurrentThreadId(); , while creating this complex object the GetCurrentThreadId() returns the ID of the SECOND THREAD, which is GOOD. Now, in my CActionDialog I receive notifications from this object usind ::SendMessage(), the SendMessage function is called from within a Worker thread that is specific to the Complex Object just created. When I receive those notifications I need to access some of that complex object values, for that I call some other functions from 'ComplexObject.DLL' which verify using the ::GetCurrentThreadId() function that the ID of the calling thread is the same as the ID of thread that created that complex object. That verification fails for me, because the functions get called using the thread ID of the MAIN THREAD, that has the Main interface GUI. Why is that? I cannot understand! (I hope I successfully explained myself).


回答1:


The problem you seem to have, from your description at least, is that whatever external API you are using via CreateAnObject, it restricts its further use to creation thread. Taking it as is, you are limited to making calls from the creation thread only. Whenever your code running on other theads, including thread hosting CWarningDlg, needs to talk to this API, you need to transfer the call to the CActionDlg thread and proceed from there.

Synchronization can be SendMessage you already do, or something safer like PostMessage with event/message completion notification.



来源:https://stackoverflow.com/questions/18336005/wtl-multithreading-multiple-interfaces-libraries

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!