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