Pausing and Resuming Threads
I am new to Multithreading and I have created a worker thread using Win32 calls. The worker thread parses a data file, and outputs data to a list box. I would like to be able to pause and resume this thread like a VCR. I have used examples of SuspendThread(HANDLE hThread) and ResumeThread(HANDLE hThread) When I hit the pause button, this locks up the entire program, and there is no recovery. Has anyone done this with signals? I have read about them, but now I am confused. Here is a snipet of code that I created for the button handlers. Any help is appreciated.
case IDC_PARSE_BUTTON:
hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)
FileRead, Params,CREATE_SUSPENDED, &threadID);
if(!hThread){Error_Code = GetLastError();}
SetThreadPriority(hThread, THREAD_PRIORITY_IDLE);
hThreadStorage = hThread;
Result = ResumeThread(hThread);
break;
case IDC_PAUSE:
butt_press++;
butt_state = butt_press%2;
if (butt_state != 0)
{//Pause Event}
else
//Resume event
if (Result != TRUE)
{Error_Code = GetLastError();}
break;
SlayerofC
[1200 byte] By [
SlayerofC] at [2007-11-18 20:31:43]

# 2 Re: Pausing and Resuming Threads
I have tried using WaitForSingleObject, but I am missing the logic on how to use it.
Here is how I am thinking it should be used.
Call SetEvent(hEvent) inside the Pause button handler, where the hEvent is a HANDLE from the CreateEvent call setting it to FALSE on the signal parameter.
Here is where I am stuck, how does the thread know that the Event has been signaled and then proceeds to wait?
In the thread do I WaitForSingleObject() There?
Please advise.
Thanks Im getting close
SlayerofC
# 3 Re: Pausing and Resuming Threads
Something like this:
GUI Thread/Main Thread
{
//initialization...
ghTerminate = CreateEvent(... );//create a manual reset event initially non-signalled.
ghParseData = CreateEvent(... ); //create a manual reset event initially signalled or non-signalled however you want
//spawn worker thread for parsing data file
//show GUI
//signal worker thread to close
SetEvent(ghTerminate);
WaitForSingleObject(hWorkerThread...); //wait for it to terminate
//cleanup
}
Worker Thread
{
HANDLE hEvents = { ghTerminate, ghParseData };
DWORD dwRet = WaitForMultipleObjects(&hEvents... );
switch(dwRet)
{
case WAIT_OBJECT_0 : // implies signal to terminate
break;
case WAIT_OBJECT_0 + 1 : //implies parse data
{
//parse next chunk.
}
break;
case WAIT_OBJECT_ABANDONED: //error conditions...
default: /// error conditions...
break;
}
}
GUIWindowProc
{
switch(msg)
{
case WM_COMMAND:
if(pause button clicked)
ResetEvent(ghParseData);
else if(play clicked)
SetEvent(ghParseData);
}
}