Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Mobile Development
  3. Mobile
  4. Thread exit problem

Thread exit problem

Scheduled Pinned Locked Moved Mobile
helpsecuritydebugging
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Cedric Moonen
    wrote on last edited by
    #1

    Hello, I have an application that has to start a new thread. In this thread I acquire and I calculate data. So, when I exit the program, I terminate the Thread but the thread doesn't exit! Here is the code: Starting the Thread:

    CMyClass::Enable() //To start the Thread
    {
    //... do some stuff here
    fEnabled = 1;
    DWORD ThreadID;
    m_hNewThread = CreateThread(
    NULL, //No Security
    0, //Same Stacksize
    MyClassThreadProc, //Thread Proc
    this, //Adress of my class
    0, //start at once
    &ThreadID);
    }

    Thread Proc function:

    DWORD WINAPI MyClassThreadProc( LPVOID pParam )
    {
    CMyClass* pObject = (CMyClass*)pParam;

    if (pObject == NULL)
    	return -1;    // illegal parameter
    
    // do something with 'pObject'
    pObject->ServiceHandler();
    
    return 0;    // thread completed successfully
    

    }

    Idle function:

    void CMyClass::ServiceHandler()
    {
    while (fEnabled)
    {
    //Do stuf here: read and calculate data !
    }
    }

    When I want to exit the Thread, I call this function:

    void CMyClass::Disable()
    {
    if (!fEnabled)
    return;

    DWORD ExitCode;
    
    fEnabled=0;
    
    //Wait for ServiceThread 
    GetExitCodeThread(m\_hNewThread,&ExitCode);
    while(ExitCode == STILL\_ACTIVE)
    	GetExitCodeThread(m\_hNewThread,&ExitCode);
    
    //Close Serial Port
    WRS232::Close();
    
    return;
    

    }

    But that doesn't work !! The while loop in the Disable function never ends !!! I have not this problem when I set a breakpoint in this loop ! Thanks for help !!

    D 1 Reply Last reply
    0
    • C Cedric Moonen

      Hello, I have an application that has to start a new thread. In this thread I acquire and I calculate data. So, when I exit the program, I terminate the Thread but the thread doesn't exit! Here is the code: Starting the Thread:

      CMyClass::Enable() //To start the Thread
      {
      //... do some stuff here
      fEnabled = 1;
      DWORD ThreadID;
      m_hNewThread = CreateThread(
      NULL, //No Security
      0, //Same Stacksize
      MyClassThreadProc, //Thread Proc
      this, //Adress of my class
      0, //start at once
      &ThreadID);
      }

      Thread Proc function:

      DWORD WINAPI MyClassThreadProc( LPVOID pParam )
      {
      CMyClass* pObject = (CMyClass*)pParam;

      if (pObject == NULL)
      	return -1;    // illegal parameter
      
      // do something with 'pObject'
      pObject->ServiceHandler();
      
      return 0;    // thread completed successfully
      

      }

      Idle function:

      void CMyClass::ServiceHandler()
      {
      while (fEnabled)
      {
      //Do stuf here: read and calculate data !
      }
      }

      When I want to exit the Thread, I call this function:

      void CMyClass::Disable()
      {
      if (!fEnabled)
      return;

      DWORD ExitCode;
      
      fEnabled=0;
      
      //Wait for ServiceThread 
      GetExitCodeThread(m\_hNewThread,&ExitCode);
      while(ExitCode == STILL\_ACTIVE)
      	GetExitCodeThread(m\_hNewThread,&ExitCode);
      
      //Close Serial Port
      WRS232::Close();
      
      return;
      

      }

      But that doesn't work !! The while loop in the Disable function never ends !!! I have not this problem when I set a breakpoint in this loop ! Thanks for help !!

      D Offline
      D Offline
      Daniel Strigl
      wrote on last edited by
      #2

      Have you added the volatile keyword to the fEnable flag? That's needed, otherwise the compiler will remove the while (fEnabled) statement during optimizing the code. Daniel ;) --------------------------- Never change a running system!

      C 1 Reply Last reply
      0
      • D Daniel Strigl

        Have you added the volatile keyword to the fEnable flag? That's needed, otherwise the compiler will remove the while (fEnabled) statement during optimizing the code. Daniel ;) --------------------------- Never change a running system!

        C Offline
        C Offline
        Cedric Moonen
        wrote on last edited by
        #3

        Hello! Thanks for reply . I forgot the volatile keyword ! But, it doesn't work with it neither :(! When I exit my application, the Pocket PC goes much much more slowly (the thread is still in use)! Any idea?

        D 1 Reply Last reply
        0
        • C Cedric Moonen

          Hello! Thanks for reply . I forgot the volatile keyword ! But, it doesn't work with it neither :(! When I exit my application, the Pocket PC goes much much more slowly (the thread is still in use)! Any idea?

          D Offline
          D Offline
          Daniel Strigl
          wrote on last edited by
          #4

          Take a look at the following code that I use in some of my projects. I create a thread at program start and terminate it when the user close the dialog.

          BOOL CMyDlg::OnInitDialog()
          {
          CDialog::OnInitDialog();

          // TODO: Add extra initialization here
          
          m\_bRunning = TRUE;
          
          // Create the thread and start it
          m\_pSendDataThread = AfxBeginThread(SendData, this,                              
                                   THREAD\_PRIORITY\_NORMAL,
                                   0, // stack size
                                   CREATE\_SUSPENDED);
          
          ASSERT\_VALID(m\_pSendDataThread);
          m\_pSendDataThread->m\_bAutoDelete = FALSE;
          m\_pSendDataThread->ResumeThread(); // start the thread
          
          return TRUE;  // return TRUE unless you set the focus to a control
                        // EXCEPTION: OCX Property Pages should return FALSE
          

          }

          UINT CMyDlg::SendData(LPVOID pParam)
          {
          CMyDlg* pDialog = reinterpret_cast(pParam);
          ASSERT(pDialog);

          if (pDialog)
              pDialog->SendData(); // send the data to the server (host)
          
          return 0;
          

          }

          void CMyDlg::SendData()
          {
          while (m_bRunning)
          {
          // do something
          }
          }

          void CMyDlg::OnDestroy()
          {
          CDialog::OnDestroy();

          // TODO: Add your message handler code here
          
          m\_bRunning = FALSE;
          
          // Wait until the thread is terminated
          WaitForSingleObject(m\_pSendDataThread->m\_hThread, INFINITE);
          delete m\_pSendDataThread;
          

          }

          Daniel ;) --------------------------- Never change a running system!

          C 1 Reply Last reply
          0
          • D Daniel Strigl

            Take a look at the following code that I use in some of my projects. I create a thread at program start and terminate it when the user close the dialog.

            BOOL CMyDlg::OnInitDialog()
            {
            CDialog::OnInitDialog();

            // TODO: Add extra initialization here
            
            m\_bRunning = TRUE;
            
            // Create the thread and start it
            m\_pSendDataThread = AfxBeginThread(SendData, this,                              
                                     THREAD\_PRIORITY\_NORMAL,
                                     0, // stack size
                                     CREATE\_SUSPENDED);
            
            ASSERT\_VALID(m\_pSendDataThread);
            m\_pSendDataThread->m\_bAutoDelete = FALSE;
            m\_pSendDataThread->ResumeThread(); // start the thread
            
            return TRUE;  // return TRUE unless you set the focus to a control
                          // EXCEPTION: OCX Property Pages should return FALSE
            

            }

            UINT CMyDlg::SendData(LPVOID pParam)
            {
            CMyDlg* pDialog = reinterpret_cast(pParam);
            ASSERT(pDialog);

            if (pDialog)
                pDialog->SendData(); // send the data to the server (host)
            
            return 0;
            

            }

            void CMyDlg::SendData()
            {
            while (m_bRunning)
            {
            // do something
            }
            }

            void CMyDlg::OnDestroy()
            {
            CDialog::OnDestroy();

            // TODO: Add your message handler code here
            
            m\_bRunning = FALSE;
            
            // Wait until the thread is terminated
            WaitForSingleObject(m\_pSendDataThread->m\_hThread, INFINITE);
            delete m\_pSendDataThread;
            

            }

            Daniel ;) --------------------------- Never change a running system!

            C Offline
            C Offline
            Cedric Moonen
            wrote on last edited by
            #5

            Ok, that works fine with AfxBeginThread and WaitForSingleObject (I just replaced these functions). Don't know why it didn't work with my method! Thanks a lot for your help ;)

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups