473,396 Members | 1,789 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

while true loop, would a semaphore work?

I am trying to create a high precision timer using the
QueryPerformanceCounter API call which works find but I want to implement a
tick event and am not sure how best to proceed.
Say the user wanted the tick event every 500 ms, the only way I can think of
to perform that is constantly querying QueryPerformanceCounter until 500 ms
has passed then raise the event but that will use 100% of the processor,
which I don't want to do. Is there any way to implement this timer without
using 100% of the processor or is there a different way to go about this I
have not thought of?
Thanks in Advance,
~Logan
Nov 15 '05 #1
7 2180
What is your tolerance for error? If you wantr high-precision timing .net
may be the wrong thing to use. If your requirements are high-enough,
Windows might be the wrong platform.

You could get a timer PCI card to get better results, but that's getting
into the extreme.

It all depends on what you mean by "high-precision."

--
gabriel
Nov 15 '05 #2
Thread.Sleep(500) :D

"Logan McKinley" <lo***@globalweb.net> wrote in message
news:uS**************@TK2MSFTNGP10.phx.gbl...
I am trying to create a high precision timer using the
QueryPerformanceCounter API call which works find but I want to implement a tick event and am not sure how best to proceed.
Say the user wanted the tick event every 500 ms, the only way I can think of to perform that is constantly querying QueryPerformanceCounter until 500 ms has passed then raise the event but that will use 100% of the processor,
which I don't want to do. Is there any way to implement this timer without using 100% of the processor or is there a different way to go about this I
have not thought of?
Thanks in Advance,
~Logan

Nov 15 '05 #3
Logan McKinley <lo***@globalweb.net> wrote:
I am trying to create a high precision timer using the
QueryPerformanceCounter API call which works find but I want to implement a
tick event and am not sure how best to proceed.
Say the user wanted the tick event every 500 ms, the only way I can think of
to perform that is constantly querying QueryPerformanceCounter until 500 ms
has passed then raise the event but that will use 100% of the processor,
which I don't want to do. Is there any way to implement this timer without
using 100% of the processor or is there a different way to go about this I
have not thought of?


How accurate do you need it to be? Thread.Sleep won't be *terribly*
accurate, but it might be good enough.

Alternatively, you could have a sort of hybrid system which says: "As
long as I'm more than 100ms away from the due time, I'll call sleep to
try to get me to 100ms away from the due time, and after that I'll
loop". (Where 100ms is a figure plucked out of thin air :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #4
Create a schedule/worker thread that just loops and calls delegates and/or
signals your mutex, event or sem.

while(true)
{
if ( threadStop )
break;
if ( needToDoStuff ) //maybe some bool that is set outside your
thread - need to sync
DoSomeStuff(); //Call delegates, signal sem, etc.
Thread.Sleep(500);
}

--
William Stacey, MVP

"Logan McKinley" <lo***@globalweb.net> wrote in message
news:uS**************@TK2MSFTNGP10.phx.gbl...
I am trying to create a high precision timer using the
QueryPerformanceCounter API call which works find but I want to implement a tick event and am not sure how best to proceed.
Say the user wanted the tick event every 500 ms, the only way I can think of to perform that is constantly querying QueryPerformanceCounter until 500 ms has passed then raise the event but that will use 100% of the processor,
which I don't want to do. Is there any way to implement this timer without using 100% of the processor or is there a different way to go about this I
have not thought of?
Thanks in Advance,
~Logan

Nov 15 '05 #5
Jon Skeet [C# MVP] <sk***@pobox.com> wrote in
news:MP************************@msnews.microsoft.c om:
How accurate do you need it to be? Thread.Sleep won't be *terribly*
accurate, but it might be good enough.


Actually thread.sleep would be quite INaccurate because it tells the Windows
sheduler to also relinquish the threads time. Of course no thread can run for
500 ms without being preempted anyways, but you see the point.
--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"
ELKNews - Get your free copy at http://www.atozedsoftware.com

Nov 15 '05 #6
Chad Z. Hower aka Kudzu <cp**@hower.org> wrote:
Jon Skeet [C# MVP] <sk***@pobox.com> wrote in
news:MP************************@msnews.microsoft.c om:
How accurate do you need it to be? Thread.Sleep won't be *terribly*
accurate, but it might be good enough.


Actually thread.sleep would be quite INaccurate because it tells the Windows
sheduler to also relinquish the threads time. Of course no thread can run for
500 ms without being preempted anyways, but you see the point.


Absolutely - which is why I suggested that if accuracy is an issue, you
ask to sleep until some margin before the due time, so that you should
wake up some time before the due time.

If you don't want the thread to relinquish any time, you might as well
just use the tight loop, but then you might get interrupted anyway...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #7
Logan,
Have you considered using the System.Timers.Timer class? Or one of the other
two timers already available in .NET?

For a discussion of the three timers available in .NET see:
http://msdn.microsoft.com/msdnmag/is...T/default.aspx

Hope this helps
Jay

"Logan McKinley" <lo***@globalweb.net> wrote in message
news:uS**************@TK2MSFTNGP10.phx.gbl...
I am trying to create a high precision timer using the
QueryPerformanceCounter API call which works find but I want to implement a tick event and am not sure how best to proceed.
Say the user wanted the tick event every 500 ms, the only way I can think of to perform that is constantly querying QueryPerformanceCounter until 500 ms has passed then raise the event but that will use 100% of the processor,
which I don't want to do. Is there any way to implement this timer without using 100% of the processor or is there a different way to go about this I
have not thought of?
Thanks in Advance,
~Logan

Nov 15 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Timothy Fitz | last post by:
http://www.python.org/moin/PythonSpeed ] "Starting with Py2.3, the interpreter optimizes 'while 1' to just a single jump. In contrast "while True" takes several more steps. While the latter is...
6
by: foldface | last post by:
Hi I am doing drag and drop between controls (listview and treeview) using mouse_event. What I am after is a book/resource to explain the behaviour below. If I do the various Win32 api calls...
30
by: Felix Kater | last post by:
Hi, in some cases like dynamic memory allocation I find it convenient to (ab-)use for() or while() like this: /* (allocate memory) */ for(;;){ /* alternatively: while(TRUE){ */
5
by: patrick | last post by:
hi all, i am looking for a way to break a while True: when pressing "s" on my keyboard. how can i do this? pat
10
by: Jason (Kusanagihk) | last post by:
To all, I have written a SerialPort class / application using C# and .Net Framework 3.0. but I have a question; since my serialPort class / application is not a Windows Form application (rather...
4
by: crochunter | last post by:
Hi, I want to read values from a text files from specified fields and use them as values to fill my methods inside the paintComponent() method. I am using for loop to do that but not able to do it...
1
by: Jose Luis Barrio | last post by:
I have an application that has a textbox and 3 buttons. With the first button I generate a random code and is showed in the textbox. The second button generates codes automatically and I want to use...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.