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. Worker Thread

Worker Thread

Scheduled Pinned Locked Moved C / C++ / MFC
helplearning
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.
  • R Offline
    R Offline
    Roger65
    wrote on last edited by
    #1

    Here is my worker thread that I create ever 100ms in the OnTimer() function, it works but uses 97% of the cpu. Any help would be appreciated. POINT pt; // Global for GetMouseProc & OnTimer. UINT GetMouseProc(LPVOID pParam) { HWND hwnd = ::GetDesktopWindow(); ::SetCapture(hwnd); ::GetCursorPos(&pt); ::ReleaseCapture(); ::ExitThread(0); return 0; } void CColorTakeDlg::OnTimer(UINT nIDEvent) { // TODO: Add your message handler code here and/or call default HWND m_hwnd = ::GetDesktopWindow(); CString str; // ======================================================================= // Get the mouse pointer position and display it // ======================================================================= DWORD ExitCode = STILL_ACTIVE; HANDLE m_hThread; if(pThread == NULL) { pThread = AfxBeginThread(GetMouseProc, this, THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED, NULL); pThread->m_bAutoDelete = FALSE; m_hThread = pThread->m_hThread; } pThread->ResumeThread(); while(ExitCode == STILL_ACTIVE) { ::GetExitCodeThread(m_hThread, &ExitCode); } delete(pThread); pThread = NULL; str.Format("X: %d, Y: %d", pt.x, pt.y); m_mouse.SetWindowText((LPCTSTR)str); Old dog learning new tricks!

    P 1 Reply Last reply
    0
    • R Roger65

      Here is my worker thread that I create ever 100ms in the OnTimer() function, it works but uses 97% of the cpu. Any help would be appreciated. POINT pt; // Global for GetMouseProc & OnTimer. UINT GetMouseProc(LPVOID pParam) { HWND hwnd = ::GetDesktopWindow(); ::SetCapture(hwnd); ::GetCursorPos(&pt); ::ReleaseCapture(); ::ExitThread(0); return 0; } void CColorTakeDlg::OnTimer(UINT nIDEvent) { // TODO: Add your message handler code here and/or call default HWND m_hwnd = ::GetDesktopWindow(); CString str; // ======================================================================= // Get the mouse pointer position and display it // ======================================================================= DWORD ExitCode = STILL_ACTIVE; HANDLE m_hThread; if(pThread == NULL) { pThread = AfxBeginThread(GetMouseProc, this, THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED, NULL); pThread->m_bAutoDelete = FALSE; m_hThread = pThread->m_hThread; } pThread->ResumeThread(); while(ExitCode == STILL_ACTIVE) { ::GetExitCodeThread(m_hThread, &ExitCode); } delete(pThread); pThread = NULL; str.Format("X: %d, Y: %d", pt.x, pt.y); m_mouse.SetWindowText((LPCTSTR)str); Old dog learning new tricks!

      P Offline
      P Offline
      PJ Arends
      wrote on last edited by
      #2

      Roger65 wrote: while(ExitCode == STILL_ACTIVE) { ::GetExitCodeThread(m_hThread, &ExitCode); } This is what is taking up the CPU time. If you really need a seperate thread here (doubt it) then you are better off using events and calling WaitForSingleObject() on that event. Even better would be to set the threads m_bAutoDelete member to TRUE and wait on the thread handle. Check out http://www.codeproject.com/threads/usingworkerthreads.asp[^], especially the section entitled "Thread Shutdown Without Polling".


      "You're obviously a superstar." - Christian Graus about me - 12 Feb '03 "Obviously ???  You're definitely a superstar!!!" mYkel - 21 Jun '04 Within you lies the power for good - Use it!


      Honoured as one of The Most Helpful Members of 2004

      R 1 Reply Last reply
      0
      • P PJ Arends

        Roger65 wrote: while(ExitCode == STILL_ACTIVE) { ::GetExitCodeThread(m_hThread, &ExitCode); } This is what is taking up the CPU time. If you really need a seperate thread here (doubt it) then you are better off using events and calling WaitForSingleObject() on that event. Even better would be to set the threads m_bAutoDelete member to TRUE and wait on the thread handle. Check out http://www.codeproject.com/threads/usingworkerthreads.asp[^], especially the section entitled "Thread Shutdown Without Polling".


        "You're obviously a superstar." - Christian Graus about me - 12 Feb '03 "Obviously ???  You're definitely a superstar!!!" mYkel - 21 Jun '04 Within you lies the power for good - Use it!


        Honoured as one of The Most Helpful Members of 2004

        R Offline
        R Offline
        Roger65
        wrote on last edited by
        #3

        The reason for the seperate thread is if the SetCapture is in the same thread as the controls window they don't get mouse messages. You say wait for the thread handle, what is it destrored? Old dog learning new tricks!

        P 1 Reply Last reply
        0
        • R Roger65

          The reason for the seperate thread is if the SetCapture is in the same thread as the controls window they don't get mouse messages. You say wait for the thread handle, what is it destrored? Old dog learning new tricks!

          P Offline
          P Offline
          PJ Arends
          wrote on last edited by
          #4

          For what you are trying to do in the example given I think you are over-thinking your problem. Why even call SetCapture to begin with? Why even have a worker thread? All you seem to want is to get the cursor coordinates every timer tick and display them on your m_mouse window. GetCursorPos does not care about which window has the capture, it justr returns the position of the cursor in screen coordinates. Why won't something like this work?

          void CColorTakeDlg::OnTimer(UINT nIDEvent)
          {
          POINT pt;
          GetCursorPos(&pt);
          CString str;
          str.Format("X: %d, Y: %d", pt.x, pt.y);
          m_mouse.SetWindowText(str);
          }

          No worker threads, no global variables, and definitely cleaner than what you have.


          "You're obviously a superstar." - Christian Graus about me - 12 Feb '03 "Obviously ???  You're definitely a superstar!!!" mYkel - 21 Jun '04 Within you lies the power for good - Use it!


          Honoured as one of The Most Helpful Members of 2004

          R 1 Reply Last reply
          0
          • P PJ Arends

            For what you are trying to do in the example given I think you are over-thinking your problem. Why even call SetCapture to begin with? Why even have a worker thread? All you seem to want is to get the cursor coordinates every timer tick and display them on your m_mouse window. GetCursorPos does not care about which window has the capture, it justr returns the position of the cursor in screen coordinates. Why won't something like this work?

            void CColorTakeDlg::OnTimer(UINT nIDEvent)
            {
            POINT pt;
            GetCursorPos(&pt);
            CString str;
            str.Format("X: %d, Y: %d", pt.x, pt.y);
            m_mouse.SetWindowText(str);
            }

            No worker threads, no global variables, and definitely cleaner than what you have.


            "You're obviously a superstar." - Christian Graus about me - 12 Feb '03 "Obviously ???  You're definitely a superstar!!!" mYkel - 21 Jun '04 Within you lies the power for good - Use it!


            Honoured as one of The Most Helpful Members of 2004

            R Offline
            R Offline
            Roger65
            wrote on last edited by
            #5

            That was version 1.0 it didn't get the cursor from the desktop, just the dialog box. You did however give me a clue, I put a Sleep(1) inside the while(exitcode == STILL_ALIVE), cpu usage dropped to 0. Old dog learning new tricks!

            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