Click to See Complete Forum and Search --> : Multi-Threading - Stopping A Thread?
Trippin_On_X
Apr 2nd, 2002, 12:40 AM
Alrighty, I'm understanding threads alot more than I was before heheh but here's my next hurdle. I created a thread and got it working fine but now I can't figure out how to stop it. I would like the thread to stop running when I click on the quit button I have setup.
case ID_QUITBTN:
?
break;
Technocrat
Apr 2nd, 2002, 10:14 AM
Can you post the code of how you are getting the thread to fire
CornedBee
Apr 2nd, 2002, 12:58 PM
Usually you pass a flag (e.g. a BOOL variable) inside the thread's params. The thread should repeatedly check this flag - if it is set, the thread returns.
Trippin_On_X
Apr 2nd, 2002, 01:39 PM
// Here's where the function's at.
void APICallAction()
{
// API Calls here
}
// Here's where I fire off the thread.
case ID_ACTIONBTN:
HANDLE APICallThread;
DWORD threadID;
APICallThread=CreateThread(0, 0, (LPTHREAD_START_ROUTINE) APICallAction, 0, 0, &threadID);
break;
Need anything else?
CornedBee
Apr 3rd, 2002, 05:46 AM
The prototype of the thread func must be
void Name (LPVOID);
no matter whether you use the argument or not. Otherwise you just might get serious problems. And it saves you from having to cast to LPTHREAD_START_ROUTINE.
Inside the LPVOID parameter you can pass things to the thread, like here:
// Here's where the function's at.
void APICallAction(LPVOID pArg)
{
PBOOL pCon = (PBOOL)pArg;
while(*pCon)
{
// Do something here
}
delete pCon;
}
// Here's where I fire off the thread.
case ID_ACTIONBTN:
HANDLE APICallThread;
DWORD threadID;
PBOOL pCon = new BOOL; // save this pointer somewhere
// maybe static in WndProc
*pCon = TRUE;
APICallThread=CreateThread(0, 0, (LPTHREAD_START_ROUTINE) APICallAction, pCon, 0, &threadID);
break;
// When you want the thread to end:
*pCon = FALSE; // thread willl terminate a few milliseconds later
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.