繁体   English   中英

从后台线程更改为主线程

[英]Change from background thread to main thread

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

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;
        }
    }
}

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM