Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   Timers outside of main proc - (http://www.programmingforums.org/showthread.php?t=15039)

pcbrainbuster Jan 25th, 2008 8:26 PM

Timers outside of main proc -
 
Sup guys,

For some reason when I call SetTimer(...) from outside my main window procedure it doesn't do anything. Why is that? How can I get through this?

Thanks.

Ancient Dragon Jan 25th, 2008 9:49 PM

Re: Timers outside of main proc -
 
post code -- its a little hard to see from where I'm sitting right now.

grumpy Jan 26th, 2008 5:06 AM

Re: Timers outside of main proc -
 
Again, the answer is RTFM: go to MSDN and do a search for "SetTimer" and then attempt to understand what the documentation says.

pcbrainbuster Jan 26th, 2008 8:47 AM

Re: Timers outside of main proc -
 
Here's the code -

:

  1. #include <windows.h>
  2.  
  3. const char *ClsName = "BasicApp";
  4. const char *WndName = "Timer Test";
  5.  
  6. LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  7. VOID SetTimerC();
  8.  
  9. HINSTANCE hInstance;
  10. HWND hWnd;
  11.  
  12. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  13. LPSTR lpCmdLine, int nCmdShow)
  14. {
  15. MSG Msg;
  16. HWND hWnd;
  17. WNDCLASSEX WndClsEx;
  18.  
  19. // Create the application window
  20. WndClsEx.cbSize = sizeof(WNDCLASSEX);
  21. WndClsEx.style = CS_HREDRAW | CS_VREDRAW;
  22. WndClsEx.lpfnWndProc = WndProc;
  23. WndClsEx.cbClsExtra = 0;
  24. WndClsEx.cbWndExtra = 0;
  25. WndClsEx.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  26. WndClsEx.hCursor = LoadCursor(NULL, IDC_ARROW);
  27. WndClsEx.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
  28. WndClsEx.lpszMenuName = NULL;
  29. WndClsEx.lpszClassName = ClsName;
  30. WndClsEx.hInstance = hInstance;
  31. WndClsEx.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
  32.  
  33. // Register the application
  34. RegisterClassEx(&WndClsEx);
  35.  
  36. // Create the window object
  37. hWnd = CreateWindow(ClsName,
  38. WndName,
  39. WS_OVERLAPPEDWINDOW,
  40. CW_USEDEFAULT,
  41. CW_USEDEFAULT,
  42. CW_USEDEFAULT,
  43. CW_USEDEFAULT,
  44. NULL,
  45. NULL,
  46. hInstance,
  47. NULL);
  48.  
  49. // Find out if the window was created
  50. if( !hWnd ) // If the window was not created,
  51. return 0; // stop the application
  52.  
  53. // Display the window to the user
  54. ShowWindow(hWnd, SW_SHOWNORMAL);
  55. UpdateWindow(hWnd);
  56.  
  57. // Decode and treat the messages
  58. // as long as the application is running
  59. while( GetMessage(&Msg, NULL, 0, 0) )
  60. {
  61. TranslateMessage(&Msg);
  62. DispatchMessage(&Msg);
  63. }
  64.  
  65. return Msg.wParam;
  66. }
  67.  
  68. LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
  69. {
  70.  
  71. switch (Msg)
  72. {
  73.  
  74.   case WM_DESTROY :
  75.  
  76.   PostQuitMessage(WM_QUIT);
  77.  
  78.   break;
  79.  
  80.   case WM_CLOSE :
  81.  
  82.   DestroyWindow(hWnd);
  83.  
  84.   break;
  85.  
  86.   case WM_TIMER :
  87.  
  88.   case 201 :
  89.  
  90.     MessageBox(hWnd, "Inner timing success!", "Report", MB_OK);
  91.  
  92.   break;
  93.  
  94.   case 202 :
  95.  
  96.     MessageBox(hWnd, "Outer timing success!", "Report", MB_OK);
  97.  
  98.   break;
  99.  
  100.   break;
  101.  
  102.   case WM_CREATE :
  103.  
  104.   SetTimer(hWnd, 201, 5000, NULL);
  105.   SetTimerC();
  106.  
  107.   break;
  108.  
  109. }
  110.  
  111. return DefWindowProc(hWnd, Msg, wParam, lParam);
  112.  
  113. }
  114.  
  115. VOID SetTimerC()
  116. {
  117.  
  118. SetTimer(hWnd, 202, 2500, NULL);
  119.  
  120. }


If you test it, you would notice that the function that was called from with in the main procedure works while the one from the function SetTimerC does not. Why is that?

Thanks.

Ancient Dragon Jan 26th, 2008 8:55 AM

Re: Timers outside of main proc -
 
If you mean line 118, then check the value of the global variable hWnd -- it was never set to anything. Line 37 set the value of hWnd which was declared on line 16. Delete line 16 and your program will probably work. You also need to delete either line 104 or 105 because you don't need them both.

grumpy Jan 26th, 2008 11:32 AM

Re: Timers outside of main proc -
 
To put in another way, the value of hWnd within WndProc() is passed as an argument (so the system generally ensures it points at a window). Within SetTimerC(), hWnd (as Dragon said) is an uninitialised global. Either pass a valid window handle as an argument to SetTimerC() or set the global so it is a valid handle.

pcbrainbuster Jan 26th, 2008 12:05 PM

Re: Timers outside of main proc -
 
Thank you guys! It works great now! :) RESOLVED


All times are GMT -5. The time now is 12:43 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC