473,756 Members | 2,996 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

High res timer

Is it possible to have a synchronous thread... actually a timer that is
beyond the 1khz/1ms resolution that .NET offers?

I want to poll the parallel port at rates beyond 1khz... about 10 to 100
times that rate if possible.

Essnetially I want to monitor the parallel port for data and display it but
I need a fast but somewhat precise way of knowing the sample rate.

Doing some tests on just a simple thread I can get speeds at around 150mhz
or so with a normal priority System.Thread. I figure that since I just need
about 1/1000 of that it shouldn't be that big a deal.

Is this possible in C#.NET? If not, is it possible to write a simple driver
in unmanaged C++ that either polls the port or hooks onto an
interrupt(synce d) to get the data to a C# app?

Thanks,
Jon
Sep 6 '07 #1
19 3445

To be honest, I guess I really need a high res timer becaues I also want to
send data. The data doesn't have to be syncronized though as the pc will be
a "master". Although I will need to send the data to the port in a timely
fashion at some configurable rate from 1khz to 100khz(the faster the
better). (jitter is not a huge concern though)

I figure that if I time a thread that, say, runs at 150mhz, I can setup a
delay to lock in on a specific rate. But in this case I'm wasting a lot of
cycles delaying the thread. If I had a Thread.Sleep that was more accurate
than 1ms then it would be a piece of cake. (I understand that theres a lot
of task switching invovled and stuff but I'm not looking for the optimal
method but something that works and doesn't bring the system to a screaching
halt).

Sep 6 '07 #2
On Sep 6, 3:46 pm, "Jon Slaughter" <Jon_Slaugh...@ Hotmail.comwrot e:
To be honest, I guess I really need a high res timer becaues I also want to
send data. The data doesn't have to be syncronized though as the pc will be
a "master". Although I will need to send the data to the port in a timely
fashion at some configurable rate from 1khz to 100khz(the faster the
better). (jitter is not a huge concern though)

I figure that if I time a thread that, say, runs at 150mhz, I can setup a
delay to lock in on a specific rate. But in this case I'm wasting a lot of
cycles delaying the thread. If I had a Thread.Sleep that was more accurate
than 1ms then it would be a piece of cake. (I understand that theres a lot
of task switching invovled and stuff but I'm not looking for the optimal
method but something that works and doesn't bring the system to a screaching
halt).

First off, the windows timer has a resolution of something like 15-45
ms (depending on the OS). Sleep(1) can last 15 ms. That's just the
way it is.

You could potentially use QueryPerformanc eCounter in some sort of spin
loop to get the resolution you want (i.e. call it multiple times and
break out of the loop when you find that you've gone beyond the time
span). Problem is you have to give up timeslices back to the OS to
keep your spin loop from grabbing all the cycles. Issue is the same
as above, sleep(0) can give up more than just the time slice to the
OS. I think there is also a Multimedia counter somewhere but IIRC,
the resolution is only 1 ms

You really should consider allowing the port to signal you when you
receive data rather than polling the port to see if there is data.

Sep 6 '07 #3

"Doug Semler" <do********@gma il.comwrote in message
news:11******** **************@ d55g2000hsg.goo glegroups.com.. .
On Sep 6, 3:46 pm, "Jon Slaughter" <Jon_Slaugh...@ Hotmail.comwrot e:
>To be honest, I guess I really need a high res timer becaues I also want
to
send data. The data doesn't have to be syncronized though as the pc will
be
a "master". Although I will need to send the data to the port in a timely
fashion at some configurable rate from 1khz to 100khz(the faster the
better). (jitter is not a huge concern though)

I figure that if I time a thread that, say, runs at 150mhz, I can setup a
delay to lock in on a specific rate. But in this case I'm wasting a lot
of
cycles delaying the thread. If I had a Thread.Sleep that was more
accurate
than 1ms then it would be a piece of cake. (I understand that theres a
lot
of task switching invovled and stuff but I'm not looking for the optimal
method but something that works and doesn't bring the system to a
screaching
halt).


First off, the windows timer has a resolution of something like 15-45
ms (depending on the OS). Sleep(1) can last 15 ms. That's just the
way it is.

You could potentially use QueryPerformanc eCounter in some sort of spin
loop to get the resolution you want (i.e. call it multiple times and
break out of the loop when you find that you've gone beyond the time
span). Problem is you have to give up timeslices back to the OS to
keep your spin loop from grabbing all the cycles. Issue is the same
as above, sleep(0) can give up more than just the time slice to the
OS. I think there is also a Multimedia counter somewhere but IIRC,
the resolution is only 1 ms

You really should consider allowing the port to signal you when you
receive data rather than polling the port to see if there is data.
And how would I do this? It would require an interrupt or polling. In each
case there are issues.

Now the interrupt method might be the best if there is no way to do high
resolution polling but does require an external clock and some way to hook
the interrupt for the port. Unfortunately then its very difficult to vary
the clock speed(I suppose its not all that difficult but does require much
more hardware than it should) and requires the use of one of the pins.

Surely what I want to do is not impossible. After all, its done all the time
such as with USB, ethernet, RS-232, parallel port, etc... Its quite simple
to do in DOS but I'd rather do it in windows and use .NET.

The reason I think polling is the easiest is that its very simple to do. The
main issue is that if I use Sleep(1) then I get something like a 250hz rate
and if I don't then its up to 150mhz. Seems kinda moronic that I can get
anything inbetween.

Jon
Sep 6 '07 #4

What I did was made an unmanaged C++ function that is a simple delay using n
nops. I figure I can profile some test code and get an average speed and
then use as a timer. I suppose it might work good enough.... or atleast
until I find a better method

Thanks,
Jon
Sep 6 '07 #5

"Jon Slaughter" <Jo***********@ Hotmail.comwrot e in message
news:Pk******** **********@news svr11.news.prod igy.net...
>
What I did was made an unmanaged C++ function that is a simple delay using
n nops. I figure I can profile some test code and get an average speed and
then use as a timer. I suppose it might work good enough.... or atleast
until I find a better method

Thanks,
Jon
Now I'm limited to about 100khz max. Just the act of inserting the a dll
call has made it 1000 times slower. I suppose this has something to do with
going from managed to unmanaged code. In any case I think it will be ok for
now.
Sep 6 '07 #6
There are a number of high resolution timers you can play with. The magic
barrier seems to be in the 1ms range though, which may not be good enough
for you.

This article looked as if it would be a good starting point:
http://msdn.microsoft.com/msdnmag/is...solutionTimer/

--
Chris Mullins, MCSD.NET, MCPD:Enterprise , Microsoft C# MVP
http://www.coversant.com/blogs/cmullins

"Jon Slaughter" <Jo***********@ Hotmail.comwrot e in message
news:mG******** ********@newssv r22.news.prodig y.net...
Is it possible to have a synchronous thread... actually a timer that is
beyond the 1khz/1ms resolution that .NET offers?

I want to poll the parallel port at rates beyond 1khz... about 10 to 100
times that rate if possible.

Essnetially I want to monitor the parallel port for data and display it
but I need a fast but somewhat precise way of knowing the sample rate.

Doing some tests on just a simple thread I can get speeds at around 150mhz
or so with a normal priority System.Thread. I figure that since I just
need about 1/1000 of that it shouldn't be that big a deal.

Is this possible in C#.NET? If not, is it possible to write a simple
driver in unmanaged C++ that either polls the port or hooks onto an
interrupt(synce d) to get the data to a C# app?

Thanks,
Jon

Sep 6 '07 #7
This MSDN article also seems to show a decent example in Managed Code:

http://msdn2.microsoft.com/en-us/library/aa964692.aspx

--
Chris Mullins, MCSD.NET, MCPD:Enterprise , Microsoft C# MVP
http://www.coversant.com/blogs/cmullins

"Jon Slaughter" <Jo***********@ Hotmail.comwrot e in message
news:mG******** ********@newssv r22.news.prodig y.net...
Is it possible to have a synchronous thread... actually a timer that is
beyond the 1khz/1ms resolution that .NET offers?

I want to poll the parallel port at rates beyond 1khz... about 10 to 100
times that rate if possible.

Essnetially I want to monitor the parallel port for data and display it
but I need a fast but somewhat precise way of knowing the sample rate.

Doing some tests on just a simple thread I can get speeds at around 150mhz
or so with a normal priority System.Thread. I figure that since I just
need about 1/1000 of that it shouldn't be that big a deal.

Is this possible in C#.NET? If not, is it possible to write a simple
driver in unmanaged C++ that either polls the port or hooks onto an
interrupt(synce d) to get the data to a C# app?

Thanks,
Jon

Sep 6 '07 #8
Well guys, this is what I've done
static void counter()

{

long startCount = PerfCount.Query PerformanceCoun ter();

double elapsedSeconds = 0;

double QPF = (double)PerfCou nt.QueryPerform anceFrequency() ;

while (true)

{

clicks++;

// Delay x seconds

/*

startCount = PerfCount.Query PerformanceCoun ter();

do

{

Delayer(1);

elapsedSeconds = (PerfCount.Quer yPerformanceCou nter() - startCount)/QPF;

} while (elapsedSeconds < 0.0000001);

*/

Delayer(100000) ;

}

}

}

I have another thread that gathers how many clicks past

Starting Thread Profiling...
18436 clicks, 9.205 khz
36567 clicks, 9.129 khz
54224 clicks, 9.024 khz
71853 clicks, 8.969 khz
89471 clicks, 8.934 khz
106669 clicks, 8.876 khz
124794 clicks, 8.901 khz
142910 clicks, 8.919 khz
161026 clicks, 8.933 khz
179568 clicks, 8.943 khz
Total Time: 20.0789 secs, Average Freq: 8.983 khz, Std Dev: 0.1 khz
Standard Jitter: 1.119%
Press any key to continue . . .

using a Delayer(0) (1 nop) gives

Starting Thread Profiling...
20622424 clicks, 10.297 mhz
40389803 clicks, 10.083 mhz
57608071 clicks, 9.588 mhz
75853991 clicks, 9.468 mhz
95928456 clicks, 9.579 mhz
116091324 clicks, 9.66 mhz
136231241 clicks, 9.717 mhz
156395445 clicks, 9.761 mhz
176444567 clicks, 9.788 mhz
196572318 clicks, 9.81 mhz
Total Time: 20.0388 secs, Average Freq: 9.775 mhz, Std Dev: 0.235 mhz
Standard Jitter: 2.405%
Press any key to continue . . .
I can control the freq from 0 to around 10mhz on my machine with usually
about 1-2% standard jitter. It always executes faster at the start for some
reason(I guess the GC starts to get in the way or something else?).

In any case, it does seem to give me the ability to control the transmission
rate even though I'm wasting a lot of cycles I think it will work for now.
Of course it doesn't help that much with monitoring the port but maybe I can
work something out.

Thanks,

Jon


Sep 7 '07 #9

"Chris Mullins [MVP]" <cm******@yahoo .comwrote in message
news:eZ******** ********@TK2MSF TNGP05.phx.gbl. ..
There are a number of high resolution timers you can play with. The magic
barrier seems to be in the 1ms range though, which may not be good enough
for you.
Well, The high performance timers can get us resolution but the main issue
is that I have to end up wasting cycles if I want a better delay and I have
to content with a somewhat large amount of jitter(which I do not think will
be a huge issue for the most part).

I guess I don't have much of a choice with windows though. I'm sure it can
be done using low level drivers but at this point I think I've found my
solution(atleas t I can move on to main part of the project).

Thanks,
Jon
Sep 7 '07 #10

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

Similar topics

2
1652
by: Arsen V. | last post by:
Hello, Did anyone have some experience with the following: 1) Need to track high volume of impressions - 20,000,000+ per day 2) Backend is SQL Server 2000 3) Webfarm of IIS with ASP.NET 4) Need to track the data as follows: DATE, TIME (resolution should be 1 hour), ID, NUMBER OF IMPRESSIONS, SOME OTHER COUNTS
2
733
by: Michael Evans | last post by:
First, we rely on a stable update rate so that our physics and dynamics calculations and integrations are based on a known interval and therefore are true-to-life. Second, the graphics positions in our image generators are updated by this dynamics application, and we need to update them at the rate they are being refreshed so that we don't get any stutter from frames being used twice or having to be thrown out. Without microsecond...
7
4985
by: LBT | last post by:
I have a window service written using VB.NET. This window service will scan folders for file and grab the file content to be inserted to SQL Server on file detection. There are altogether 18 folders to be watched. Each folder is assigned with a timer for watching purpose. Hence there will be 18 timers and each timer is set to elapse on every second. Problem here, once the window service is installed and started, the CPU usage is very...
8
1566
by: bob | last post by:
I am unsure how to approach a C# windows App. Essentially I want to do this. I want to write an app that reads from database_A performs some calculations updates the pretty dials on the screen then writes the 'cooked' data to database_B (probably not in that order). Also I will have some buttons on this main window that open some other winforms so I can capture other bits of info and adjust the main window's pretty dials and textboxes...
0
1756
by: bg_ie | last post by:
Hi all, I'm writing a .net Com object to read messages from an external bus. Also, under certain situations, I output messages to the bus in order to generate bus load, for testing purposes. An idea of my code is supplied below, and as you can see I use Stopwatch to generate an accurate timer. DoLoop is my timer callback and it is called with an interval of 20ms. When doing the bus load I need the timer to perform as well as possible,...
19
15003
by: John | last post by:
The table below shows the execution time for this code snippet as measured by the unix command `time': for i in range(1000): time.sleep(inter) inter execution time ideal 0 0.02 s 0 s 1e-4 4.29 s 0.1 s 1e-3 4.02 s 1 s
7
502
by: Andrew Wan | last post by:
I found this excellent High Speed Timer (in Pascal). I compiled it (using Turbo Pascal 7 and it runs fine): http://www.sorucevap.com/bilisimteknolojisi/programcilik/pascal/ders.asp?207995 and same High Speed Timer here too: http://groups.google.com/group/comp.lang.pascal/browse_thread/thread/92e9398f16c10ba4/e67ff3cf587648ef?lnk=st&q=inline(%24CD)+inline(%241C)+inline(%249C)&rnum=1&hl=en#e67ff3cf587648ef I converted it to C (using p2c),...
17
7967
by: Cesar | last post by:
Hello people. I'm having a Winform app that contains a webbrowser control that keeps navigating from one page to another permanentrly to make some tests. The problem I'm having is that after a while, the application is using more than 100 or 150 Mb in RAM, and if I let it continue, it can leave the system without memory. I've been watching in some pages that other people has the same problem with this control when keep navigating for a...
8
12674
alexis4
by: alexis4 | last post by:
Hello! I need to slide a picture from right to left. So I need a timer event. When this event comes, I decrease picturebox’s X position by 1. The thing is that I need a timer faster than 1ms. After some searching, I came up with the following demo code (timer is set for 1ms): using System; using System.Drawing; using System.Windows.Forms; using System.Windows.Threading; //WindowsBase.dll
0
9431
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9255
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9819
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9689
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8688
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6514
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5119
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5289
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3780
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.