在产生条件时完成螺纹。 C / C ++ [英] Finish a thread when condition is produced. C/C++

查看:140
本文介绍了在产生条件时完成螺纹。 C / C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C代码,检查鼠标的左边是否按下。它工作正常,但我不知道如何解除挂钩并退出当前线程调用final()函数。

I have a C code that checks if the left buttom of the mouse has been pressed. It works fine but I don't know how to unhook and exit the current thread after calling the final() function.

这是代码:

LRESULT CALLBACK mouseProc(int nCode, WPARAM wParam, LPARAM lParam)
{
static int count = 0;
static int finalNum;
if (count==0){
    //////Generate random number 
    srand(time(NULL)); // Seed the time
    finalNum = rand() % (150 - 50) + 50; // Generate the number, assign to variable.
    ////////////////////////////////////////////////////////////////////////////
}
//int count = 0;
MOUSEHOOKSTRUCT * pMouseStruct = (MOUSEHOOKSTRUCT *)lParam;
    if (pMouseStruct != NULL){
        if (wParam == WM_LBUTTONDOWN)
        {
            count++;
            printf("%d",count);
            if (count==finalNum){ // user clicked random times the mouse so we launch the final function
                printf("\ndone!\n");
                final();

            }
            printf("clicked");
        }
        printf("Mouse position X = %d  Mouse Position Y = %d\n", pMouseStruct->pt.x, pMouseStruct->pt.y);
    }
    return CallNextHookEx(hMouseHook, nCode, wParam, lParam);
}

DWORD WINAPI MyMouseLogger(LPVOID lpParm)
{
    HINSTANCE hInstance = GetModuleHandle(NULL);
    // here I put WH_MOUSE instead of WH_MOUSE_LL
    hMouseHook = SetWindowsHookEx(WH_MOUSE_LL, mouseProc, hInstance, NULL);
    MSG message;

    while (GetMessage(&message, NULL, 0, 0)) {
        TranslateMessage(&message);
        DispatchMessage(&message);
    }

    UnhookWindowsHookEx(hMouseHook);
    return 0;
}

void custom_delay(){

}

int main(int argc, char *argv[])
{
    int count = 0;
    HANDLE hThread;
    DWORD dwThread;
    //////Generate random number to call a function after rand() number of clicks
    srand(time(NULL)); // Seed the time
    int finalNum = rand() % (150 - 50) + 50; // Generate the number, assign to variable.
    ////////////////////////////////////////////////////////////////////////////

    printf("%d", finalNum);
    hThread = CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)MyMouseLogger, (LPVOID)argv[0], NULL, &dwThread);
    if (hThread)
        return WaitForSingleObject(hThread, INFINITE);
    else
        return 1;
    }
printf("Keep going");
}

我试过ExitThread但它关闭所有的线程,而不只是MyMouseLogger进程打印继续。

I've tried with ExitThread(0); but it closes all the threads and not only the MyMouseLogger process to print "Keep going".

我应该怎么办?

谢谢您的帮助!

推荐答案

你只是问如何安全地停止线程运行。

This isn't really related to hooks as such; you are rather just asking how to safely stop a thread from running.

ExitThread()永远不是一个好的选择。所以实现自己的方式安全地停止线程。在这种情况下,可以通过例如在创建线程之前通过CreateEvent()创建事件来完成。然后让你的线程每次在循环中检查这个事件:

ExitThread() is never a good choice. So implement your own way to safely stop the thread. In this case, it can be done by for example creating an event through CreateEvent() before creating the thread. Then have your thread check for this event each time in the loop:

while (WaitForSingleObject(hevent_stopthread, 0) != WAIT_OBJECT_0 &&)
       GetMessage(&message, NULL, 0, 0))

hevent_stopthread 将HANDLE设为您的自定义事件。

where hevent_stopthread it the HANDLE to your custom event.

然后只需从final()函数中设置此事件。你的main()函数将等待,直到线程自己完成,通过返回0.一旦发生,你可以通过调用CloseHandle()来清理事件。

Then simply set this event from the final() function. Your main() function will wait until the thread has finished by its own, by returning 0. Once that happens, you can clean up the event with a call to CloseHandle().

这篇关于在产生条件时完成螺纹。 C / C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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