Pause and resume main thread in C ++ for Windows

I need to be able to pause and resume the main thread in a Windows C ++ application. I used

handle = GetCurrentThread();
SuspendThread(handle);

and then where should be renewed

ResumeThread(handle);

while the suspension works, the resumption of this does not occur. I have other threads that are paused and resumed without problems, is there something that is different from the main thread.

I worked a lot with threads, working in C # and Java, but this is the first time I have done something in C ++, and I think this is completely different.

+3
source share
4 answers

"handle", GetCurrentThread() ? psuedo. , DuplicateHandle

HANDLE hThread = OpenThread(THREAD_ALL_ACCESS, FALSE, GetCurrentThreadId());
+12

GetCurrentThread "-", . DuplicateHandle , .

. http://msdn.microsoft.com/en-us/library/ms683182%28VS.85%29.aspx

+6

- CreateEvent WaitForSingleObject, SetEvent .

+2
source

And here is an example that shows what some of the people used to offer.

#include <stdio.h>
#include <tchar.h>
#include <windows.h>
#include <process.h>

HANDLE g_hMainThread;
void TheThread(void *);

int _tmain(int argc, _TCHAR* argv[])
{
    g_hMainThread = OpenThread(THREAD_ALL_ACCESS,
                               FALSE,
                               GetCurrentThreadId());
    printf( "Suspending main thread.\n" );
    _beginthread(TheThread, 0, NULL);
    SuspendThread(g_hMainThread);
    printf( "Main thread back in action.\n" );
    return 0;
}

void TheThread(void *)
{
    DWORD dwStatus = ResumeThread(g_hMainThread);
    DWORD dwErr = GetLastError();
    printf("Resumed main thread - Status = 0x%X, GLE = 0x%X.\n",
           dwStatus,
           dwErr );
}
+2
source

Source: https://habr.com/ru/post/1727390/


All Articles