Click here to Skip to main content
15,887,350 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
Questionxmlhttp improper response Pin
Ash_VCPP3-Jul-09 2:28
Ash_VCPP3-Jul-09 2:28 
AnswerRe: xmlhttp improper response Pin
Stuart Dootson3-Jul-09 3:34
professionalStuart Dootson3-Jul-09 3:34 
Questionthreads and their time slots. [modified] Pin
Souldrift2-Jul-09 23:45
Souldrift2-Jul-09 23:45 
AnswerRe: threads and their time slots. Pin
Stuart Dootson3-Jul-09 0:31
professionalStuart Dootson3-Jul-09 0:31 
GeneralRe: threads and their time slots. [modified] Pin
Souldrift3-Jul-09 1:17
Souldrift3-Jul-09 1:17 
GeneralRe: threads and their time slots. Pin
Stuart Dootson3-Jul-09 1:58
professionalStuart Dootson3-Jul-09 1:58 
GeneralRe: threads and their time slots. Pin
Souldrift3-Jul-09 2:43
Souldrift3-Jul-09 2:43 
GeneralRe: threads and their time slots. Pin
Stuart Dootson3-Jul-09 3:30
professionalStuart Dootson3-Jul-09 3:30 
I put together a wee program demonstrating waitable timer usage:

#include "stdafx.h"
#include <iostream>
#include <Windows.h>

void ReportTime(const char* message, LONGLONG const& when)
{
   std::cout << message << double(when)/10000.0 << std::endl;
}

VOID CALLBACK DoSendHere(__in_opt  LPVOID lpArgToCompletionRoutine,
                         __in      DWORD dwTimerLowValue,
                         __in      DWORD dwTimerHighValue)
{
   LARGE_INTEGER const& ftSleepStart = *(LARGE_INTEGER*)lpArgToCompletionRoutine;
   LARGE_INTEGER liTimer;
   liTimer.LowPart = dwTimerLowValue;
   liTimer.HighPart = dwTimerHighValue;
   ReportTime("DoSendHere::Timer slept for ", liTimer.QuadPart-ftSleepStart.QuadPart);
   delete lpArgToCompletionRoutine;
}

int SendLoop()
{
   LARGE_INTEGER ftStart;
   GetSystemTimeAsFileTime((FILETIME*)&ftStart);
   HANDLE hTimer = 0;
   hTimer = ::CreateWaitableTimer(0, TRUE, L"WaitableTimer");

   LARGE_INTEGER ftNow;
   LARGE_INTEGER ftWaitTill;
   GetSystemTimeAsFileTime((FILETIME*)&ftNow);
   while(1)
   {
      ReportTime("SendLoop::Now = ", ftNow.QuadPart-ftStart.QuadPart);

      ftWaitTill.QuadPart = ftNow.QuadPart + 5000000I64;
      SetWaitableTimer(hTimer, &ftWaitTill, 0, &DoSendHere, new LARGE_INTEGER(ftNow), FALSE);
      ftNow = ftWaitTill;
      ::SleepEx(INFINITE, TRUE);
   }
}

int _tmain(int argc, _TCHAR* argv[])
{
   SendLoop();
	return 0;
}


This is intended to set a timer for 500ms in the future and sleep until the timer's triggered, at which point a) the completion routine is called, and b) the SleepEx call exits.

I get this output (on Windows XP SP 3):

SendLoop::Now = 0
DoSendHere::Timer slept for 499.978
SendLoop::Now = 500
DoSendHere::Timer slept for 499.955
SendLoop::Now = 1000
DoSendHere::Timer slept for 499.933
SendLoop::Now = 1500
DoSendHere::Timer slept for 499.91
SendLoop::Now = 2000
DoSendHere::Timer slept for 499.888
SendLoop::Now = 2500
DoSendHere::Timer slept for 499.866
SendLoop::Now = 3000
DoSendHere::Timer slept for 499.843
SendLoop::Now = 3500
DoSendHere::Timer slept for 499.821
SendLoop::Now = 4000
DoSendHere::Timer slept for 499.798
SendLoop::Now = 4500
DoSendHere::Timer slept for 499.776
SendLoop::Now = 5000
DoSendHere::Timer slept for 499.754
SendLoop::Now = 5500
DoSendHere::Timer slept for 499.731
SendLoop::Now = 6000
DoSendHere::Timer slept for 499.709
SendLoop::Now = 6500
DoSendHere::Timer slept for 499.686
SendLoop::Now = 7000
DoSendHere::Timer slept for 515.288
SendLoop::Now = 7500
DoSendHere::Timer slept for 499.642

You can see that the timer triggers approximately 40 microseconds before the due time. This is interrupted when the processor's busy (for example, when I rebuild the software), as shown by the sleep period of 515ms. This is because Windows will not interrupt an active thread when signalled - so your time could be delayed by as much as a time-slice quantum.

Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

GeneralRe: threads and their time slots. Pin
Souldrift3-Jul-09 5:44
Souldrift3-Jul-09 5:44 
AnswerRe: threads and their time slots. Pin
Roger Stoltz3-Jul-09 2:09
Roger Stoltz3-Jul-09 2:09 
GeneralRe: threads and their time slots. Pin
Souldrift3-Jul-09 2:50
Souldrift3-Jul-09 2:50 
AnswerRe: threads and their time slots. Pin
Keith Worden3-Jul-09 5:29
Keith Worden3-Jul-09 5:29 
GeneralRe: threads and their time slots. Pin
Souldrift3-Jul-09 6:04
Souldrift3-Jul-09 6:04 
AnswerRe: threads and their time slots. Pin
Souldrift3-Jul-09 6:07
Souldrift3-Jul-09 6:07 
GeneralRe: threads and their time slots. Pin
Keith Worden3-Jul-09 6:18
Keith Worden3-Jul-09 6:18 
GeneralRe: threads and their time slots. Pin
Souldrift3-Jul-09 6:39
Souldrift3-Jul-09 6:39 
AnswerRe: threads and their time slots. Pin
Souldrift3-Jul-09 6:48
Souldrift3-Jul-09 6:48 
AnswerRe: threads and their time slots. Pin
Souldrift3-Jul-09 7:34
Souldrift3-Jul-09 7:34 
GeneralRe: threads and their time slots. Pin
Stuart Dootson3-Jul-09 8:33
professionalStuart Dootson3-Jul-09 8:33 
GeneralRe: threads and their time slots. Pin
Roger Stoltz3-Jul-09 8:41
Roger Stoltz3-Jul-09 8:41 
GeneralRe: threads and their time slots. Pin
Stuart Dootson3-Jul-09 8:50
professionalStuart Dootson3-Jul-09 8:50 
GeneralRe: threads and their time slots. Pin
Roger Stoltz3-Jul-09 8:55
Roger Stoltz3-Jul-09 8:55 
GeneralRe: threads and their time slots. Pin
Souldrift5-Jul-09 21:37
Souldrift5-Jul-09 21:37 
GeneralRe: threads and their time slots. Pin
Stuart Dootson5-Jul-09 21:43
professionalStuart Dootson5-Jul-09 21:43 
GeneralRe: threads and their time slots. [modified] Pin
Souldrift5-Jul-09 22:19
Souldrift5-Jul-09 22:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.