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
sneeuwAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

lif081199Commented:
Use a timer :

UINT gTimer;

// the callback function
VOID CALLBACK TimerProc(HWND hwnd,UINT uMsg,UINT idEvent,DWORD dwTime)
{
    // this function will be called every second
    // write here to the disk,...
}

to start the timer from your app :
gTimer = SetTimer(0,0,1000,TimerProc);

to kill the timer from your app:
KillTimer(0,gTimer);

Good luck,
Lionel
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
KangaRooCommented:
Nederlands?
0
sneeuwAuthor Commented:
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 ;-)
0
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

lif081199Commented:
Of course ! Just ask.
0
KangaRooCommented:
Ha, altijd leuk. Kijk ook eens naar

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

(wel vreselijk lang)
0
sneeuwAuthor Commented:
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
0
sneeuwAuthor Commented:
Hi Lionel,

"Of course ! Just ask."

Asking !

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

Thanks,
Peter
0
nietodCommented:
>> 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?
0
sneeuwAuthor Commented:
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 ;-)
0
lif081199Commented:
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.
0
nietodCommented:
>> 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.
0
sneeuwAuthor Commented:
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
0
sneeuwAuthor Commented:
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
0
lif081199Commented:
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.

0
lif081199Commented:
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.
0
lif081199Commented:
Ok, I'm waiting for your code.
0
sneeuwAuthor Commented:
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 ;-)
0
sneeuwAuthor Commented:
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::  

0
lif081199Commented:
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.




0
sneeuwAuthor Commented:
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
0
lif081199Commented:
The word "static" should only be in the header, not in the cpp.
0
sneeuwAuthor Commented:
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'.
0
lif081199Commented:
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.
0
sneeuwAuthor Commented:
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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.