Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 25th, 2008, 8:26 PM   #1
pcbrainbuster
Programmer
 
Join Date: Dec 2007
Posts: 93
Rep Power: 1 pcbrainbuster is on a distinguished road
Exclamation 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.
pcbrainbuster is offline   Reply With Quote
Old Jan 25th, 2008, 9:49 PM   #2
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 532
Rep Power: 4 Ancient Dragon is on a distinguished road
Re: Timers outside of main proc -

post code -- its a little hard to see from where I'm sitting right now.
Ancient Dragon is offline   Reply With Quote
Old Jan 26th, 2008, 5:06 AM   #3
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,209
Rep Power: 5 grumpy is on a distinguished road
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.
grumpy is offline   Reply With Quote
Old Jan 26th, 2008, 8:47 AM   #4
pcbrainbuster
Programmer
 
Join Date: Dec 2007
Posts: 93
Rep Power: 1 pcbrainbuster is on a distinguished road
Re: Timers outside of main proc -

Here's the code -

c Syntax (Toggle Plain Text)
  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.

Last edited by Ancient Dragon; Jan 26th, 2008 at 8:53 AM. Reason: add line numbers
pcbrainbuster is offline   Reply With Quote
Old Jan 26th, 2008, 8:55 AM   #5
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 532
Rep Power: 4 Ancient Dragon is on a distinguished road
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.
Ancient Dragon is offline   Reply With Quote
Old Jan 26th, 2008, 11:32 AM   #6
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,209
Rep Power: 5 grumpy is on a distinguished road
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.
grumpy is offline   Reply With Quote
Old Jan 26th, 2008, 12:05 PM   #7
pcbrainbuster
Programmer
 
Join Date: Dec 2007
Posts: 93
Rep Power: 1 pcbrainbuster is on a distinguished road
Re: Timers outside of main proc -

Thank you guys! It works great now! RESOLVED
pcbrainbuster is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Return type - main() / ISO C standard sharadpro C 2 Mar 14th, 2007 6:02 AM
Call the class main function quantalfred Java 6 Jul 23rd, 2006 1:38 PM
OOT Program Examples Sane Other Scripting Languages 4 Nov 25th, 2005 12:06 AM
noob questions about main() format linuxpimp20 C 16 Jul 11th, 2005 7:48 AM
Returning a value from a variable to the main function colt C 3 Apr 28th, 2005 7:56 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:17 PM.

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