Results 1 to 19 of 19

Thread: Freezing

  1. #1

    Thread Starter
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    How are you using the timer? Callback functions are the most reliable method, and usually don't cause freezing.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  2. #2
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    oops...I read my post before reading your reply, and noticed that I wasn't using "the" timer, I'm making my own, but my while loop is freezing it.

    Code:
    void Timer(bool on)  {
    	while(on==true)  {
    		long ConstTime;
    		long RefreshTime;
    		ConstTime=GetTickCount();
    		while(pauseme==false)  {
    			RefreshTime = GetTickCount();
    			if(RefreshTime-ConstTime > 1000)  {
    				secs++;;
    				if(secs>=60) {
    					secs=0;
    					mins++;
    				}
    				if(secs<=9)  {
    					strcat(finalTime, itoa(mins, buffer, 10));
    					strcat(finalTime, ":0");
    					strcat(finalTime, itoa(secs, buffer, 10));
    					SendMessage(ghWnd_Timer, WM_SETTEXT, 0, (LPARAM) (LPCSTR) finalTime);
    				}
    				if(secs>9)  {
    					strcat(finalTime, itoa(mins, buffer, 10));
    					strcat(finalTime, ":");
    					strcat(finalTime, itoa(secs, buffer, 10));
    					SendMessage(ghWnd_Timer, WM_SETTEXT, 0, (LPARAM) (LPCSTR) finalTime);
    				}
    				int i;
    				for(i=1; i<=90; i++)  {
    					finalTime[i] = '\0';
    				}
    				ConstTime=GetTickCount();
    			}
    		}
    		Sleep(1);
    	}
    }

  3. #3

    Thread Starter
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    It could be getting optimised out

    The most likely explanation is that your program is stuck inside that loop, which means that it is no longer able to perform its message loop, thus freezing the application. This is why you use SetTimer, because it runs in an external thread, your program can update itself without worrying about this.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  4. #4
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    Could you give me an example?


    Could I do this?: Put the message loop into a function, and where it is right now I could call the function. So I could call it from my loop? Probably not, just a stab in the dark

  5. #5

    Thread Starter
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    A possibility is something like this (pseudocode):
    Code:
    msgloop_func {
        getmessage
        translate
        dispatch
    }
    
    timer_func {
        // that code you had
        each time round, call msgloop_func to ensure any messages are processed
    }
    
    winmain {
        init
        loop {
            msgloop_func
        }
    }
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  6. #6
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    Code:
    C:\Windows\Desktop\C c++\Mediaboy\mediaboy.cpp(201) : warning C4508: 'WinMain' : function should return a value; 'void' return type assumed
    C:\Windows\Desktop\C c++\Mediaboy\mediaboy.cpp(263) : error C2065: 'hInstance' : undeclared identifier
    I'm getting these errors. Heres the code:
    Code:
    bool active;
    active=true;
    int MessageLoop();
    
    winmain.... {
         ...
         ...
         while(active==true)  {
              MessageLoop();
         }
    }
    int Timer()  {
      //timer code is in here
    }
    int MessageLoop()  {
        //message loop directly copied from bottom of 
        //winmain
    }

  7. #7

    Thread Starter
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Where it says hInstance isn't defined, substitute GetModuleHandle(NULL). The first one's easy. At the end of WinMain, return 0;
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  8. #8
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    in my while loop to keep calling the message loop I just said return MessageLoop()...but it still freezes...I mean the static control keeps updating but can't push any buttons and I can't move the form.

  9. #9

    Thread Starter
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    The main problem is that you're only in one thread -- the execution point can only be in one place at a time (without using setjmp/longjmp which isn't recommended). Why can't you use the normal timer functions?
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  10. #10
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    I don't know them

  11. #11

    Thread Starter
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Well, far be it from me to let you go without knowing these incredible functions
    Code:
    void __stdcall TimerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime) {
        // Do something here
    }
    
    WinMain(...) {
        int nID = SetTimer(NULL, 0, 1000 /* Timeout in ms */, TimerProc);
    
        // Message loop
    
        KillTimer(NULL, nID);
    }
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  12. #12

    Thread Starter
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Oh, and if it whinges about UINT_PTR change it to UINT...I'm just using the PSDK defs because I don't have the old (obsolete ) ones any more.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  13. #13
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    could you explain exactly what that does? Do they have to be around the message loop? Can I put them into a function?

    by them I mean:
    Code:
    int nID = SetTimer(NULL, 0, 1000 /* Timeout in ms */, TimerProc);
        KillTimer(NULL, nID);

  14. #14

    Thread Starter
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    The timer runs from when you call SetTimer to when you call KillTimer. As a result, if you call KillTimer straight after SetTimer it's a tad pointless

    Idea: have 2 buttons on a window. When one is clicked, call SetTimer. When the other is clicked, call KillTimer. In your timer procedure update a static control with the time
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  15. #15
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    errors: [code]C:\Windows\Desktop\C c++\Win32_clock\clock.cpp(36) : error C2065: 'nID' : undeclared identifier
    C:\Windows\Desktop\C c++\Win32_clock\clock.cpp(39) : error C2065: 'TimerProc' : undeclared identifier
    C:\Windows\Desktop\C c++\Win32_clock\clock.cpp(94) : error C2373: 'TimerProc' : redefinition; different type modifiersC:\Windows\Desktop\C c++\Win32_clock\clock.cpp(36) : error C2065: 'nID' : undeclared identifier
    C:\Windows\Desktop\C c++\Win32_clock\clock.cpp(39) : error C2065: 'TimerProc' : undeclared identifier
    C:\Windows\Desktop\C c++\Win32_clock\clock.cpp(94) : error C2373: 'TimerProc' : redefinition; different type modifiers[/code

  16. #16
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    errors:
    Code:
    C:\Windows\Desktop\C c++\Win32_clock\clock.cpp(36) : error C2065: 'nID' : undeclared identifier
    C:\Windows\Desktop\C c++\Win32_clock\clock.cpp(39) : error C2065: 'TimerProc' : undeclared identifier
    C:\Windows\Desktop\C c++\Win32_clock\clock.cpp(94) : error C2373: 'TimerProc' : redefinition; different type modifiersC:\Windows\Desktop\C c++\Win32_clock\clock.cpp(36) : error C2065: 'nID' : undeclared identifier
    C:\Windows\Desktop\C c++\Win32_clock\clock.cpp(39) : error C2065: 'TimerProc' : undeclared identifier
    C:\Windows\Desktop\C c++\Win32_clock\clock.cpp(94) : error C2373: 'TimerProc' : redefinition; different type modifiers

  17. #17

    Thread Starter
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Read my post a few posts up...
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  18. #18
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    I still get the errors by changing it to UNIT

  19. #19

    Thread Starter
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Oh f**k it, I'll talk to you on MSN
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width