Question

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).

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top