Results 1 to 5 of 5

Thread: Multi-Threading - Stopping A Thread?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2002
    Location
    Miami
    Posts
    22

    Multi-Threading - Stopping A Thread?

    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;

  2. #2
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    Can you post the code of how you are getting the thread to fire
    MSVS 6, .NET & .NET 2003 Pro
    I HATE MSDN with .NET & .NET 2003!!!

    Check out my sites:
    http://www.filthyhands.com
    http://www.techno-coding.com


  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Mar 2002
    Location
    Miami
    Posts
    22
    PHP Code:
    // 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(00, (LPTHREAD_START_ROUTINEAPICallAction00, &threadID);
    break; 
    Need anything else?

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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:

    Code:
    // 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
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width