从后台线程更改为主线程

时间:2015-11-19 09:26:45

标签: c++ multithreading

所以我创建了一个新线程,以便操作在后台线程上运行:

void ProcessCall(char *szJSON)
{       
    #ifdef _WIN32
        HANDLE hThread = CreateThread(NULL, 0, ProcessOnNewThread, szJSON, 0, NULL);
        CloseHandle(hThread);
        hThread = NULL;
    #endif
}

namespace callEventCallbacks
{
   void OnEventDetected(const char *szEvent)
   {
        // Make sure the callback function is specified
        if (callEventInterface.ProcessCallBackJSInterfaceResponse == NULL)
           return;

        if (!strcmp(szEvent, "EVENT_DETECTED"))
           iOLibInterface.ProcessCallBackJSInterfaceResponse (szEventDetectedCallback, NULL, 0);
   }
}

namespace
{   
    DWORD WINAPI ProcessOnNewThread(LPVOID lpvParam)
    {
         // Obtain the string JSON from the parameter
         char *szJSON = (char *)lpvParam;

        // Attempt to parse the JSON to rapidjson Document structure - this allocates memory for the json document, the string can be freed after parsing it
        Document jsonDocument;
        bool validJson = !jsonDocument.Parse<0>(szJSON).HasParseError();

        // Obtain the parameters from the JSON based on the operation type
        if (!strcmp(szOperation, "RegisterForCardEvent"))
        {
            if (jsonDocument.HasMember("EventName") && jsonDocument.HasMember("Callback"))
            {
                 // Obtain the event name and callback function name
                const char *szEvent = jsonDocument["EventName"].GetString();
                const char *szCallback = jsonDocument["Callback"].GetString();

                 // Store the callback in a required buffer and register the callback for a required card event
                 if (!strcmp(szEvent, "EVENT_DETECTED"))
                 {
                     SPRINTF_S(callEventCallbacks::szEventDetectedCallback, sizeof(callEventCallbacks::szEventDetectedCallback), szCallback);
                     callEvent.RegisterForCardEvent(callEventCallbacks::OnEventDetected);
                  }
             }
         }
     }
}

然后我想将后台线程切换到主线程并继续操作:(让switch语句在主线程而不是后台线程上运行)

namespace
{
    CJSCallDoc *globalDocument;
    void ProcessCallBackJSInterfaceResponse (const char *szCallback, const char **arrayOfParameters, int arraySize)
    {
        switch (arraySize)
        {
        case 0:
            globalDocument->m_webPage.CallJScript(szCallback);
            break;
        case 1:
            globalDocument->m_webPage.CallJScript(szCallback, arrayOfParameters[0]);
            break;
        case 2:
            globalDocument->m_webPage.CallJScript(szCallback, arrayOfParameters[0], arrayOfParameters[1]);
            break;
        case 3:
            globalDocument->m_webPage.CallJScript(szCallback, arrayOfParameters[0], arrayOfParameters[1], arrayOfParameters[2]);
            break;
        default:
            break;
        }
    }
}

有没有人知道如何做到这一点,并能够给我一个想法?

1 个答案:

答案 0 :(得分:2)

在Windows中,如果您希望后台线程在GUI线程上触发操作,最常见的事情是将自定义窗口消息发布或发送到主线程的消息队列并对其作出反应。 / p>

PostMessage是异步的,因此后台线程不会等待完成,而SendMessage是同步的。