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. General Programming
  3. C / C++ / MFC
  4. Threads

Threads

Scheduled Pinned Locked Moved C / C++ / MFC
tutorial
5 Posts 3 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.
  • K Offline
    K Offline
    krishna Vuppala
    wrote on last edited by
    #1

    How to use TerminateThread() function in program. iam used like this but the thread is not stopped. CWinThread* m_pNuThread; void CThrdDlg::OnButton2() { // TODO: Add your control notification handler code here CString string; m_NuStart.GetWindowText(string); if(string =="START") { m_pNuThread = AfxBeginThread(NuThread, this); m_NuStart.SetWindowText("STOP"); } else { TerminateThread(m_pNuThread, 0); m_NuStart.SetWindowText("START"); } } UINT NuThread(LPVOID lParam) { CThrdDlg *dlg = (CThrdDlg*)lParam; //int i=0; CString str; for(int i=0;i<=100;i++) { str.Format("%d",i); dlg->m_NuEBox.SetWindowText(str); Sleep(200); if(i == 100) i=0; } return 0; } kp.Vuppala

    CPalliniC 1 Reply Last reply
    0
    • K krishna Vuppala

      How to use TerminateThread() function in program. iam used like this but the thread is not stopped. CWinThread* m_pNuThread; void CThrdDlg::OnButton2() { // TODO: Add your control notification handler code here CString string; m_NuStart.GetWindowText(string); if(string =="START") { m_pNuThread = AfxBeginThread(NuThread, this); m_NuStart.SetWindowText("STOP"); } else { TerminateThread(m_pNuThread, 0); m_NuStart.SetWindowText("START"); } } UINT NuThread(LPVOID lParam) { CThrdDlg *dlg = (CThrdDlg*)lParam; //int i=0; CString str; for(int i=0;i<=100;i++) { str.Format("%d",i); dlg->m_NuEBox.SetWindowText(str); Sleep(200); if(i == 100) i=0; } return 0; } kp.Vuppala

      CPalliniC Offline
      CPalliniC Offline
      CPallini
      wrote on last edited by
      #2

      krishna Vuppala wrote:

      How to use TerminateThread() function in program. iam used like this but the thread is not stopped.

      Thread is not stopped because you're doing a mistake on passing the first argument to TerminateThread function. It must be the thread HANDLE and not the pointer to che CWinThread class. Hence you have to use:

      TerminateThread(m_pNuThread->m_hThread, 0);

      BTW: as stated by the documentation you should avoid to use TerminateThread (it must be only the last resort), there are other ways to stop smoothly a thread, for instance setting and (added) flag in your thread loop control condition, i.e.:

      UINT NuThread(LPVOID lParam)
      {
      //...
      for(int i=0; i <= 100 && bKeepRunning;i++)
      //..
      }

      Hence you can simply set bKeepRunning=false outside the thread to gracefully stop it. Hope that helps. :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

      In testa che avete, signor di Ceprano?

      K 1 Reply Last reply
      0
      • CPalliniC CPallini

        krishna Vuppala wrote:

        How to use TerminateThread() function in program. iam used like this but the thread is not stopped.

        Thread is not stopped because you're doing a mistake on passing the first argument to TerminateThread function. It must be the thread HANDLE and not the pointer to che CWinThread class. Hence you have to use:

        TerminateThread(m_pNuThread->m_hThread, 0);

        BTW: as stated by the documentation you should avoid to use TerminateThread (it must be only the last resort), there are other ways to stop smoothly a thread, for instance setting and (added) flag in your thread loop control condition, i.e.:

        UINT NuThread(LPVOID lParam)
        {
        //...
        for(int i=0; i <= 100 && bKeepRunning;i++)
        //..
        }

        Hence you can simply set bKeepRunning=false outside the thread to gracefully stop it. Hope that helps. :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

        K Offline
        K Offline
        krishna Vuppala
        wrote on last edited by
        #3

        Mr.CPallini sir , iam very Thankful to you..iam using 2 buttons in a Dailog with two threads.it works well. now iam willing two generate two buttons with Single thread.is there any idea pls share with me sir... kp.Vuppala

        CPalliniC 1 Reply Last reply
        0
        • K krishna Vuppala

          Mr.CPallini sir , iam very Thankful to you..iam using 2 buttons in a Dailog with two threads.it works well. now iam willing two generate two buttons with Single thread.is there any idea pls share with me sir... kp.Vuppala

          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #4

          Sorry I really cannot understand your requirements, please explain better. :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

          In testa che avete, signor di Ceprano?

          H 1 Reply Last reply
          0
          • CPalliniC CPallini

            Sorry I really cannot understand your requirements, please explain better. :)

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

            H Offline
            H Offline
            Hamid Taebi
            wrote on last edited by
            #5

            I think I could decode his question.:laugh: Mr.CPallini sir , He has two buttons on the dialog and when he clicks each button it makes a thread they work very well now he wants that either buttons makes a single thread is it possible and if yes do you have any idea? (what do you think it was a good decode;))

            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