How are you using the timer? Callback functions are the most reliable method, and usually don't cause freezing.
Printable View
How are you using the timer? Callback functions are the most reliable method, and usually don't cause freezing.
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);
}
}
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.
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 :)
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'm getting these errors. Heres the code: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
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
}
Where it says hInstance isn't defined, substitute GetModuleHandle(NULL). The first one's easy. At the end of WinMain, return 0; :)
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. :confused:
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 don't know them :rolleyes:
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);
}
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 :p) ones any more.
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);
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 :rolleyes:
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 :)
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
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
Read my post a few posts up...
I still get the errors by changing it to UNIT
Oh f**k it, I'll talk to you on MSN :rolleyes: