Thread: Killing a thread which is waiting

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    8

    Killing a thread which is waiting

    I'm creating a thread like this:
    m_pCListenThreadList[0] = new CListenThread();
    m_pCListenThreadList[0]->CreateThread(CREATE_SUSPENDED, 0);
    m_pCListenThreadList[0]->SetParent(this, &m_SerialListenControl, &m_SerialListen, "COM1");
    m_pCListenThreadList[0]->ResumeThread();

    Within this thread I'm using WaitCommEvent(m_tComm, &dwEvtMask, &overlapped); to listen to the serial port.

    This is how I end the thread:
    m_pCListenThreadList[0]->SuspendThread();
    m_pCListenThreadList[0]->End();
    CloseHandle(m_pCListenThreadList[0]);
    ::TerminateThread(m_pCListenThreadList[0],1);
    delete m_pCListenThreadList[0] ;

    But the thread wont terminate unless something is coming to the serial port. It waits at WaitCommEvent. If something comes it terminates but I want to kill it even if nothing has come.

    How do you do this?

    /Andreas

  2. #2
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    170
    I found this on a FAQ page which you could try.


    How do I get WaitCommEvent to return when my program quits?

    If you call WaitCommEvent from a secondary thread, then when you want to quit, call SetCommMask(handle, 0). This sets the event parameter to 0, and cancels the waiting function.

    http://freespace.virgin.net/neil.sca...#WaitCommEvent

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Waiting For A Thread To Idle
    By SMurf in forum Windows Programming
    Replies: 2
    Last Post: 10-10-2008, 06:31 PM
  2. Thread Prog in C language (seg fault)
    By kumars in forum C Programming
    Replies: 22
    Last Post: 10-09-2008, 01:17 PM
  3. CreateThread ?!
    By Devil Panther in forum Windows Programming
    Replies: 13
    Last Post: 11-15-2005, 10:55 AM
  4. pointer to main thread from worker thread?
    By draegon in forum C++ Programming
    Replies: 2
    Last Post: 10-27-2005, 06:35 AM
  5. Critical Sections, destroying
    By Hunter2 in forum Windows Programming
    Replies: 4
    Last Post: 09-02-2003, 10:36 PM