Hi,
I am trying to impliment a System.Threading.Sleep(8) function and i am
expecting to return thead after 8 millsecs.
However thead is inly rurning after 15 mills on XP pcs and working as
it should on vista.
Reason i figure out so far is the System is only updating time only
ones in every 15 mills. I verified by calling SYSTIME structure from
stdlib call;
To solve the problem i use StopWatch class to loop untill elapsed time
to reach 8 mills which delays running thead to waite accurate 8 mills.
But this is causing processor usage to 99% which is blocking other
threads to do their job.
Here is the code block causing the problem
===================================
void ACTSound::SendAudioOut(){
array<unsigned char>^ ml_data_list;
array<unsigned char>^ ml_data_chunk = gcnew array<unsigned char>(64);
DateTime^ ml_T_last_sent;
DateTime^ ml_now;
while(true){
if(m_data_out->Length != 0){
m_mutex->WaitOne();
ml_data_list = m_data_out;
m_mutex->ReleaseMutex();
for(int i =0;i<ml_data_list->Length; i=i+64)
{
if((ml_data_list->Length-i)>= 64){
m_mutex->WaitOne();
System::Array::Copy(ml_data_list,i,ml_data_chunk,0,64);
m_mutex->ReleaseMutex();
}
else{
m_mutex->WaitOne();
System::Array::Copy(ml_data_list,i,ml_data_chunk,0,(ml_data_list-
>Length-i));
m_mutex->ReleaseMutex();
}
if((System::Environment::OSVersion)->Version->Major >= 6){
m_media_skt->UpDateAudioOut(ml_data_chunk,ml_data_chunk-
>Length);
m_th_audio_out->Sleep(8);// if
vista no problem
}
else{
Delay8ms(); // a waiting
mechanisam for XP pc with 15 mills
granularituy
}
}
}
}
}
void ACTSound::Delay8ms()
{
System::Diagnostics::Stopwatch^ stopwatch =
System::Diagnostics::Stopwatch::StartNew();
stopwatch->Start();
//waite 8 milli secs
while (stopwatch->IsRunning)
{
if(stopwatch->ElapsedMilliseconds >=8){
stopwatch->Stop();
stopwatch->Reset();
}
else{
System::Threading::Thread::CurrentThread->Sleep(1); //
this bit is
causing to wake up thread after 15 mills is i take this off system is
going 100 % usage.
}
}
}