hello all,
is it possible to have a delay period of 17nS for an application in vb.net???

please help

Dani AI

Generated

Short answer for : a VB.NET program running in user mode on a general-purpose Windows PC cannot reliably generate a 17 ns delay or toggle a pin at 30 MHz. 's Thread.Sleep idea is millisecond-level and won't work; was right to point toward Stopwatch for measurement, but Stopwatch can measure short intervals — it does not make user-mode code deterministic at the nanosecond scale.

Important clarification: DateTime/TimeSpan "ticks" are 100 ns units, but Stopwatch is driven by the high-resolution performance counter and its tick period is 1 / Stopwatch.Frequency seconds. The following VB.NET spin-wait shows how to convert Stopwatch ticks to nanoseconds and wait a tiny interval, but it only demonstrates measurement/attempted waiting — it will not give deterministic hardware timing and it burns CPU:

' Busy-wait example — not deterministic, for measurement only
Dim waitNs As Long = 17
Dim nsPerTick As Double = 1000000000.0 / Stopwatch.Frequency
Dim ticksNeeded As Long = CLng(Math.Ceiling(waitNs / nsPerTick))
Dim sw As Stopwatch = Stopwatch.StartNew()
Dim startTicks As Long = sw.ElapsedTicks
While (sw.ElapsedTicks - startTicks) < ticksNeeded
    ' spin
End While
sw.Stop()

Why this is impractical: OS scheduling, interrupts, CPU frequency scaling, core migration and I/O latency all introduce jitter and preemption far larger than 17 ns. A 30 MHz clock has a 33.33 ns period — expecting the PC to bit-bang that reliably from user space is unrealistic. Parallel ports (if even present) and USB-parallel adapters cannot guarantee those speeds; kernel drivers or direct port I/O improve latency but still do not reach true nanosecond determinism.

Recommended approach: push the timing down to hardware. Use an FPGA/CPLD or a dedicated microcontroller/timer peripheral or a DAQ/PCIe I/O board to generate and sample 30 MHz signals. Verify with a scope or logic analyzer. If staying with the PC is mandatory, treat it as a control interface only and let external hardware handle the precise timing.

Recommended Answers

All 7 Replies

try this:

Thread.Sleep(17*1000 / (1000*1000*1000))

argument takes time in millisecond. So, 3 times 1000 is to create 10^9 and 17*1000 is to remove its arguments' millisecond.

hello,

this will give 17uSec right???

thanks a lot

Did you see the constructor, it says (int millisecondTimeout). so it assumes every thing in millisecond. to cancel millisecond effect I multiplied it with 1000.

Dear Mr.Ayyub,

yes i understand.
thanks a lot. you are amazing.
i hope this will work. i can check it after tomorrow

Did you see the constructor, it says (int millisecondTimeout). so it assumes every thing in millisecond. to cancel millisecond effect I multiplied it with 1000.

And you have an expression "17*1000 / (1000*1000*1000)" which gives 0.000017 i.e. 17 nsec.
But, if the constructor takes an integer (int millisecondTimeout), value 0.000017 will be actually zero.

Here's a thread nanosecond delay and a quote from the thread:

The closest you can come, is by using a Stopwatch. But again, it "only" support ticks, and one tick is 100 nanoseconds.

This means that PC hardware, as seen from the .NET, can't measure times like 17 nsec.

One other thing, from the OS point, is the thread where "the delay is executed". Assuming you would manage to construct a "magical loop" which has an execution time of 17 nsec. and that loop is run in the UI thread, how can you be sure that OS doesn't swap out, for one reason or other, your loop in the middle of the execution?

Happy New Year 2011

hello,

so what am supposed to do i need to interface switches that has a 30MHz clock frequency using my parallel port??? am very new to programming, do how can i make this possible ??

please help

thanks

I suggest reading the link I gave first. You can also google similar articles. Not articles related to VB.NET itself but more like engineering articles that talk about controlling devices with PC.

It may be that you need some additional tools to VB.NET. I mean you may have to use a lower level language i.e. assembler to get precise enough timings. You could also check if Windows API provides any methods for high precise timings.

HTH

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.