Board index » cppbuilder » Sleep works differently between Win2K and XP

Sleep works differently between Win2K and XP


2006-11-02 03:52:13 PM
cppbuilder35
I've an application written in C++ (BCB 6) which has a timer thread. I de-schedule this thread with a Sleep(1) to allow other parts of the application to run. (Using Sleep(0) just caused the thread to be re-entered immediately). Up to now this has been developed and run on Windows2000 machines. I've recently tried to migrate this to a Windows XP machine but the Sleep call seems to cause the rest of the application (the display) to lock up. I've tried Sleep with 0, 1 and 100 as parameters, all with the same effect. The rest of the application only runs if the Sleep is now commented out. Does anyone know any differences between 2000 and XP that could accunt for this (and better still any solution)?
 
 

Re:Sleep works differently between Win2K and XP

"Andy Scarle" < XXXX@XXXXX.COM >wrote in message
Quote
Using Sleep(0) just caused the thread to be re-entered immediately
Then you don't have any other threads running that are waiting for their own
time slices to become available.
Quote
I've recently tried to migrate this to a Windows XP machine but the
Sleep call seems to cause the rest of the application (the display) to
lock up.
Calling Sleep() in a worker thread will not do that. Something else has to
be happening.
Quote
The rest of the application only runs if the Sleep is now commented out.
Please show your actual code.
Gambit
 

Re:Sleep works differently between Win2K and XP

In part of my application I want to check and process messages from an I/O board every millisecond so I change the tick rate to be a millisecond, and then have a Sleep in the timer thread to let the display/keyboard/mouse get a look-in.
First off I set up a fast tickrate for the system using:-
...
// Get current system time - just for a check in debugging
GetSystemTimeAdjustment( &adjustment,
&clockInterval,
&adjustmentDisabled );
// Adjust the timer tick to be 1mS (10000 * 100nS)
if( SetSystemTimeAdjustment( 10000, FALSE ) == 0 )
{
FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM, (void *)0, GetLastError(), 0, tempstr, 128, (const int
*)0 ) ;
MessageDlg( tempstr,
mtWarning,
TMsgDlgButtons() << mbOK,
0);
}
// Get current system time - just for a check in debugging
GetSystemTimeAdjustment( &adjustment,
&clockInterval,
&adjustmentDisabled );
}
if( timeBeginPeriod( 1 ) == TIMERR_NOCANDO )
{
MessageDlg("Timer Resolution setting - failed",
mtWarning,
TMsgDlgButtons() << mbOK,
0);
}
}
and start my timer thread with, (which is called as part of the FormCreate callback for the main applications form):-
Th = new TTestTimerThread(true) ;
Th->Resume() ;
the main bits of the timer thread is:-
// -----------------------------------------------------------------------------
void __fastcall TTestTimerThread::DoIt(void)
{
inTime = timeGetTime() ;
lastTime = inTime ;
// set some flags based on the timer
.....
// call a couple of routines to check an I/O boards status
.....
Sleep(1) ;
}
// -----------------------------------------------------------------------------
void __fastcall TTestTimerThread::Execute(void)
{
bool infinite ;
infinite = true ;
while(infinite)
{
Synchronize( DoIt ) ;
if(Terminated == true)
{
infinite = false ;
return;
}
}
}
//---------------------------------------------------------------------------
 

{smallsort}

Re:Sleep works differently between Win2K and XP

"Andy Scarle" < XXXX@XXXXX.COM >wrote in message
Quote
In part of my application I want to check and process messages
from an I/O board every millisecond so I change the tick rate to
be a millisecond, and then have a Sleep in the timer thread to let
the display/keyboard/mouse get a look-in.
Rather than using Sleep() in a loop, try using timeSetEvent() instead. It
runs a hig-speed multimedia timer thread.
Quote
MessageDlg( tempstr,
MessageDlg() is not thread-safe, as it displays a VCL TForm. Use the API
MessageBox() instead.
Quote
bool infinite ;
Why bother with that variable? Just use the Terminated property directly
instead, ie:
while( !Terminated )
Quote
Synchronize( DoIt ) ;
Well, no wonder your code is so slow. Get rid of the Synchronize(). That
is forcing the bulk of your thread code to run in the context of the main
thread, which defeats the purpose of using a worker thread at all.
Gambit