PDA

Click to See Complete Forum and Search --> : Freezing


parksie
Apr 27th, 2001, 07:23 AM
How are you using the timer? Callback functions are the most reliable method, and usually don't cause freezing.

SteveCRM
Apr 27th, 2001, 08:29 AM
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.

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);
}
}

parksie
Apr 27th, 2001, 12:06 PM
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.

SteveCRM
Apr 27th, 2001, 04:28 PM
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 :)

parksie
Apr 27th, 2001, 04:52 PM
A possibility is something like this (pseudocode):

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
}
}

SteveCRM
Apr 27th, 2001, 05:35 PM
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:

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
}

parksie
Apr 27th, 2001, 05:37 PM
Where it says hInstance isn't defined, substitute GetModuleHandle(NULL). The first one's easy. At the end of WinMain, return 0; :)

SteveCRM
Apr 27th, 2001, 10:15 PM
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:

parksie
Apr 28th, 2001, 01:37 PM
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?

SteveCRM
Apr 28th, 2001, 05:44 PM
I don't know them :rolleyes:

parksie
Apr 28th, 2001, 05:50 PM
Well, far be it from me to let you go without knowing these incredible functions ;)

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);
}

parksie
Apr 28th, 2001, 05:53 PM
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.

SteveCRM
Apr 29th, 2001, 05:02 PM
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:
int nID = SetTimer(NULL, 0, 1000 /* Timeout in ms */, TimerProc);
KillTimer(NULL, nID);

parksie
Apr 30th, 2001, 12:03 PM
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 :)

SteveCRM
May 1st, 2001, 04:13 PM
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

SteveCRM
May 1st, 2001, 04:13 PM
errors: 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

parksie
May 1st, 2001, 04:18 PM
Read my post a few posts up...

SteveCRM
May 1st, 2001, 04:32 PM
I still get the errors by changing it to UNIT

parksie
May 1st, 2001, 04:34 PM
Oh f**k it, I'll talk to you on MSN :rolleyes: