Link to home
Start Free TrialLog in
Avatar of sneeuw
sneeuwFlag for Belgium

asked on

Less CPU consuming alternative for Sleep() ???

Hi guys,

(Using Borland cpp Builder 4).

I have a disc accessing sequence that needs to be executed every second (or half second or ...).
Using Sleep(1000) doesn't seem a good idea.  All processes in my App wait and no progress is made during the Sleep state.
What is a good alternative ??

Thanks,
Peter
ASKER CERTIFIED SOLUTION
Avatar of lif081199
lif081199

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of KangaRoo
KangaRoo

Nederlands?
Avatar of sneeuw

ASKER

Thanks Lionel ,

Seems OK.
Can't test it now but I trust it's complete, so I'll assign the points to you.  
Hope you don't mind an extra message to this thread should I have problems with it !

Hi KangaRoo,
Yep ... een Belg ... Een van de goeie ;-)
Of course ! Just ask.
Ha, altijd leuk. Kijk ook eens naar

https://www.experts-exchange.com/jsp/qShow.jsp?ta=lounge&qid=10111117 
      - or -
https://www.experts-exchange.com/Q.10111117 (non-cookie login)

(wel vreselijk lang)
Avatar of sneeuw

ASKER

Lionel,

Problems ...
Your suggestion doesn't compile.

gTimer = SetTimer(0,0,1000,TimerProc);

TimerProc isn't accepted as a pointer to the callback function.

I experimented with casting to (TIMERPROC) and using the & operator

gTimer = SetTimer(0,0,1000,(TIMERPROC) &TimerProc);

But no success.

Do we have a situation here with hidden parameters and all ??

Please advice !!
Thanks,
Peter
Avatar of sneeuw

ASKER

Hi Lionel,

"Of course ! Just ask."

Asking !

Any suggestion on the fact that the code doesn't compile ?

Thanks,
Peter
>> Can't test it now but I trust it's complete,
>> so I'll assign the points to you.
Bad idea.  There's no harm in waiting. And if the the answer is not complete or correct you've wasted the points.

>> TimerProc isn't accepted as a pointer
>>to the callback function
This should be a pointer to a timer callback procedure that you wrote.  Did you write this procedure?  How did you declare it?
Avatar of sneeuw

ASKER

Thanks Nietod for jumping in.
Correct ... I am a succer ;-)
I shouldn't have assigned the points so early.

Anyway, If you can help me this would mean a lot to me.
I'll be happy to open a question afterwards for you to collect the points !
Is 150 points OK ?

I wrote the CallBack routine exactly as Lionel did in the example above.
I did try some other declarations (__closure, static, ...) but couldn't get it to work.

Are the callback routine parameters at all supported by this function ??
Although the Help uses the same function.

That 'declaration' stuff keeps hounting me ;-)
Sorry for being off, I was in vacancy :)

Could you please send me a snippet of your code ? Those lines are from my own code, and work well.

Here's my email : lfumery@nordnet.fr

Other point : If you prefer to give points to  anybody else, just tell me, and I will give the points myself to this person. I don't want to become a cheater :)

Have a good day,
Lionel.
>> If you can help me this would mean a lot to me
It looks like if is back, and I'm sure he can help you.  But if not, let me know.

But he needs to see the declaration for your timer callback procedure.  By the way, just to take a stab at the problem, this cannot be a non-static member procedure.  It has to be a global procedure, or a static member procedure.
Avatar of sneeuw

ASKER

Hi Lif,

Good you're back.
I see no need for you to give your points to somebody else !
I trust your intentions were OK !

I don't have code 'of my own' anymore.
I tried a little test app, but since it didn't work I threw it away ;-)

I used the code exactly as you mentioned.  Same syntax and all.
Except maybe :

There were two functions :
void TForm1::OnClickButton1 (.....)
void TForm1::OnClickButton2 (.....)

One with the SetTimer and one with KillTimer functionality

UINT gTimer;  was declared globally in the header file.


I first tried :
VOID CALLBACK TForm1::TimerProc(HWND hwnd,UINT uMsg,UINT idEvent,DWORD dwTime)
{
x++ ; // Stupid test, x declared globally
}


And next started experimenting with other declaration types for the callback function.

Question.
I guess I must use TForm1:: for this function also ????
Although I remember trying without also.
More info on this is welcome too !

Peter
Avatar of sneeuw

ASKER

I'll also try to put the code together again this evening and will mail it to you if you think it will help.

Peter
Ha, you use it in a class... So, like Nietod said, the timer callback can not be a non-static function.

Declare it like this :

class TForm1
{
   ....
 static VOID CALLBACK TimerProc(HWND hwnd,UINT uMsg,UINT idEvent,DWORD dwTime);
   ....
};

Try and tell me !

Lionel.

You can also use it as a global function (without TForm1::, as you said).

In this case, you don't need to declare it in your class header, and there is no problem.
Ok, I'm waiting for your code.
Avatar of sneeuw

ASKER

Hi Lionel,

If I am not mistaken I already tried :

Declare it like this :

class TForm1
{
   ....
 static VOID CALLBACK TimerProc(HWND hwnd,UINT uMsg,UINT idEvent,DWORD dwTime);
   ....
};



But ...
I'll try again this evening.
God knows what I had screwed up already ;-)
Avatar of sneeuw

ASKER

Is it :

{
   ....
 static VOID CALLBACK TimerProc(HWND hwnd,UINT uMsg,UINT idEvent,DWORD dwTime);
   ....
};


or :

{
   ....
 static VOID TForm1::CALLBACK TimerProc(HWND hwnd,UINT uMsg,UINT idEvent,DWORD dwTime);
   ....
};


Difference :
Notice the use of :  TForm1::  

You mean
{
   ....
 static VOID CALLBACK TForm1::TimerProc(HWND hwnd,UINT uMsg,UINT idEvent,DWORD dwTime);
   ....
};
(for the second solution) ? (with TForm1:: just before TimerProc(...) )

The two notations are valid, if you are in the class header.




Avatar of sneeuw

ASKER

True !
Sorry (I was not paying enough attention I guess)

What I meant was ... in the cpp file itself ... : Do I use TForm1:: or not for the Callback function ?

So, in the Class Header :

class TForm1
{
   ....
 static VOID CALLBACK TimerProc(HWND hwnd,UINT uMsg,UINT idEvent,DWORD dwTime);
   ....
};

and in the cpp File :

static VOID CALLBACK TForm1::TimerProc(HWND hwnd,UINT uMsg,UINT idEvent,DWORD dwTime)
{
x++ ; // Stupid test, x declared globally
}

?
Peter
The word "static" should only be in the header, not in the cpp.
Avatar of sneeuw

ASKER

Hi lif,

Thanks for the mail and thanks for the adjustments in my code, but it makes NO difference !

The same errors keep popping Up :

[C++ Error] main.cpp(24): E2034 Cannot convert 'void (__stdcall *)(void *,unsigned int,unsigned int,unsigned long)' to 'int (__stdcall *)()'.
[C++ Error] main.cpp(24): E2343 Type mismatch in parameter 'lpTimerFunc' in call to '__stdcall SetTimer(void *,unsigned int,unsigned int,int (__stdcall *)())'.
[C++ Error] main.cpp(35): E2316 '__stdcall TForm1::TimerProc(void *,unsigned int,unsigned int,unsigned long)' is not a member of 'TForm1'.
Hum... very curious... Can you send me the doc about SetTimer, from the Borland documentation ? I'd like to compare it with the Visual Studio version.
And about the timer callback function too, please.
Avatar of sneeuw

ASKER

Well ...
The documentation is the same as yours. (Microsoft help file).

Borland cpp also has a Timer Object (VCL class).  I guess I can use that one pretty easy.  (I'm actually sure of it)
It's just ... I wanted to incorporate the timing in a class that would NOT include VCL class stuff ... .

But I'll use it anyway.
Thanks for the effort an time you put in Lionel !

Peter