|
acerunner316 wrote: Is there a way to accurately predict the actual time it takes to post a message?
It doesn't take long to post a message as it's simply queued. The delay comes into play because of the contents of the queue, the speed at which the receiving thread calls GetMessage and processes the message and thread scheduling.
acerunner316 wrote: Is there another way I can force the function to execute at specified time intervals, and accurately?
Probably the most accurate way would be to use a dedicated high priority thread which calls the function directly then sleeps, in a loop. Naturally if the approach is feasible or not in your case depends on what your function does.
acerunner316 wrote: there something like time()
Check out GetTickCount .
Steve
|
|
|
|
|
Stephen Hewitt wrote: Probably the most accurate way would be to use a dedicated high priority thread which calls the function directly then sleeps, in a loop. Naturally if the approach is feasible or not in your case depends on what your function does.
The reason I am posting a message and not directly calling the function is because the function references members of the main class, and I remember reading somewhere that you can't do that in a thread. So how can I create a dedicated thread that calls the function directly.
Thanks.
|
|
|
|
|
That's not an easy question to answer without detailed knowledge about what you're trying to do and how you've gone about doing it.
Steve
|
|
|
|
|
Basically, my program is sampling data from an external USB device. The data is displayed in the window as well as logged to a file. I want to sample and log the data at a specified sampling rate. In this case, I chose every 10ms. The retrieval in done through API calls in the function that's responding to the windows message posted by the thread. My logic was that if I post the message every 10ms, the function will run every 10ms and therefore sampling rate will be 10ms. Clearly, this is not the case.
Hopefully that clears things up.
|
|
|
|
|
There are a number of approaches I'd consider:
The first is to abandon the idea of having “super accurate” callbacks. Instead just use a reasonably accurate mechanism, such as SetTimer or your current scheme, but record the time of each sample along with the sample data.
The second would be to use a high priority thread like I described earlier to do all the sampling and fill buffers which can be accessed by the UI thread – with appropriate synchronization.
On a side note be aware that posting messages from another thread is probably only marginally more accurate then using SetTimer .
Steve
|
|
|
|
|
well, it doesn't have to be super accurate, but 6ms delay out of the specified 10ms is huge. 1 or 2 ms might be acceptable.
Can you explain how I would use SetTimer to solve my problem, where would I call it?
Also can you explain how to use a high priority thread. Would I be able to access member variables/functions of the main class? Does it operate differently than a worker thread.
|
|
|
|
|
acerunner316 wrote: Can you explain how I would use SetTimer to solve my problem, where would I call it?
Calling SetTimer wouldn't solve your problem. I was just pointing out that using SetTimer instead of posting messages from a worker thread would be simpler and only marginally less accurate.
acerunner316 wrote: Also can you explain how to use a high priority thread. Would I be able to access member variables/functions of the main class? Does it operate differently than a worker thread.
When I said a "high priority" thread I simply meant a worker thread with a high priority. A thread's priority can be set using SetThreadPriority . Normally having a thread with a really high priority is a bad idea but since our thread spends most or its life asleep and when it is awake just takes a sample then goes back to sleep it’s ok. In this context the high priority is just to reduce the scheduling overhead so that our thread is chosen by the scheduler earlier than others when the sleep time has expired. As to accessing member variables of a class that’s a complex question which would require more detailed knowledge of your application.
Steve
|
|
|
|
|
acerunner316 wrote: Is there a way to accurately predict the actual time it takes to post a message?
no. and your process could get preempted at any time.
acerunner316 wrote: Is there another way I can force the function to execute at specified time intervals, and accurately?
not really. WM_TIMER isn't very accurate (or reliable).
acerunner316 wrote: Is there something like time() that would return ms instead of seconds?
QueryPerformanceCounter[^] might help with that.
|
|
|
|
|
Chris Losinger wrote: QueryPerformanceCounter[^] might help with that.
I think that's shooting a bit over the top. GetTickCount() is probably just about right for what he's trying to time.
--
Not based on the Novel by James Fenimore Cooper
|
|
|
|
|
he was talking about accuracy in the 10-20ms range. GetTickCount can't get near that.
|
|
|
|
|
So.. uh, the system timer resolution is too low? Lucky me that I don't have to do anything multimedia related, because I was under the impression that a timer which is given the attribute system would be the most accurate one. Thanks for the heads up - who knows? I might have to deal with issues like this some day...
--
Verletzen zerfetzen zersetzen zerstören
Doch es darf nicht mir gehören
Ich muss zerstören
|
|
|
|
|
it's updated every ~15ms to 50ms (depending on the version of Windows). so, it is measuring time in the ms range, but the difference between two consecutive calls will never be less than ~15ms.
QueryPerformanceCounter is much more accurate.
|
|
|
|
|
How will I use QueryPerformanceCounter as a way of sampling data? I will still need a thread to loop a function call. And in that function will check QueryPerformanceCounter. But if posting a message takes longer than 6ms. Then QueryPerformanceCounter can only be as accurate as that.
Can you provide a code sample? Im fairly new to windows programming. I am basically learning from sample code that I find online. Thanks.
|
|
|
|
|
acerunner316 wrote: I will still need a thread to loop a function call. And in that function will check QueryPerformanceCounter.
right
acerunner316 wrote: But if posting a message takes longer than 6ms. Then QueryPerformanceCounter can only be as accurate as that.
right
maybe this article can help... scroll down to the bottom for a message-loop / QueryPerfCounter sample.
|
|
|
|
|
sorry, i'm still having trouble understanding. In code sample at the link you gave, I can see that the function is monitoring for any messages and checking for the time. But there is the message being generated from, and how?
|
|
|
|
|
acerunner316 wrote: But there is the message being generated from, and how?
the message comes from code outside the loop. this is pretty close to an old-style C Windows message loop. Windows is passing messages (button down, repaint, keydown, etc) to the application, which the app grabs, when it sees one, and starts processing with that Peek/Translate/DispathMessage bit - other code is responsible for handling the specific msgs. otherwise, every 40ms, the app does a run through its Render/Move/etc stuff, then goes back to looking for messages.
|
|
|
|
|
Isn't this similar to what I'm already doing, posting messages every 10ms? Because what I'm doing is posting the message repeatedly in a loop every 10ms. And then I have a function mapped to handle that message ON_MESSAGE(WM_UPDATE_DATA, OnUpdateData) . So the only difference I see is instead of using ON_MESSAGE, I will have my own function that loops to check for the message posted an respond to it. Is this right? Will this make the difference of the 6ms delay? Would I have to post this message like every 1ms in order to get the 1ms resolution?
|
|
|
|
|
acerunner316 wrote: Isn't this similar to what I'm already doing, posting messages every 10ms?
pretty much. but Sleep simply isn't that accurate. Sleep uses the system clock, which has a resolution closer to 15ms per tick in most modern versions of Windows. and, it's entirely possible for your sleeping thread to get preempted by another thread, further delaying its awakening.
i'm not sure what the overhead in PostMessage / ON_MESSAGE amounts to. but they're asynchronous anyway; apps respond to messages when they get around to it, not the instant they appear in their message. the time from PostMessage to the actual response is (i believe) indeterminate.
|
|
|
|
|
Chris Losinger wrote: pretty much. but Sleep simply isn't that accurate. Sleep uses the system clock, which has a resolution closer to 15ms per tick in most modern versions of Windows. and, it's entirely possible for your sleeping thread to get preempted by another thread, further delaying its awakening.
In that case, what do I use to loop posting of a message?
|
|
|
|
|
honestly, i don't know. Windows really isn't a real-time OS, and as far as I know, there's not a lot of high-level support for the kind of timing precision you seem to want. maybe you could check an open source game or audio application (ex. Audacity) that you could plunder for ideas.
|
|
|
|
|
|
acerunner316 wrote: I am using Sleep(10) in my thread to post a windows message every 10ms.
Sleep(10) doesn't sleep your thread fro 10 ms, as windows is not realtime system... actually it sleep time range between 10 ms to 54 ms depending on current load on the system and processor speed
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
Support CRY- Child Relief and you
|
|
|
|
|
Generally Windows NT and later have a timer interrupt of 10ms. However the HAL for some systems has set it to use 15ms. This means you can't sleep for less than that amount using the timeouts for Sleep() and Wait...() calls.
Anyone who thinks he has a better idea of what's good for people than people do is a swine.
- P.J. O'Rourke
|
|
|
|
|
1. Are you sure your function which is posting message takes '0'ms to execute ?
I raise this question because, if your calling code has a lot to do and then sleep 10 ms, the interval between subsequent calls would be
(Sleep time + Time taken to execute other part of the function).
If you are very specific about the response time, i dont think post message is the right way to address such issuesbecause the latency of the response is not guaranted.
You can use Events or a Semaphore-give-release technique to decrease the latency further.
but the technique you need to choose depends on your applcation.
Haribabu.
|
|
|
|
|
I don't know if it takes 0ms, but there is nothing else to my code other than repeatedly posting messages.
UINT CNOx1000MonitorDlg::FThreadLoop (LPVOID PptrToClass)<br />
{<br />
CMainWindowDlg *LptrClass = static_cast<CMainWindowDlg *>(PptrToClass);<br />
HWND *ptrWindowHandle = new HWND;<br />
*ptrWindowHandle = LptrClass->GetSafeHwnd();<br />
<br />
while(LptrClass->mbolThreadExecute)<br />
{<br />
::PostMessage(*ptrWindowHandle, WM_UPDATE_CONTROLS, 0, 0);<br />
::Sleep(10);<br />
}<br />
delete ptrWindowHandle;<br />
return 0;<br />
}
|
|
|
|