Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

1ms timer calling functions

Posted on 2016-08-09
3
Medium Priority
?
162 Views
Last Modified: 2016-08-13
Hi,
For testing purposes I have this timer that calls a function every 1ms and adds +1 to a counter. This code works perfectly fine as its calling windows native code but is there anything in the .NET framework even close to 1ms or shall I stick to using native calls?

Public Class Form1
    Dim mHandler As TimerEventHandler
    Dim iCount As Long = 0
    Dim mTimerId As Integer

    <DllImport("winmm.dll")>
    Private Shared Function timeSetEvent(delay As Integer, resolution As Integer, handler As TimerEventHandler, user As IntPtr, eventType As Integer) As Integer
    End Function

    <DllImport("winmm.dll")>
    Private Shared Function timeBeginPeriod(millisecond As Integer) As Integer
    End Function

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        timeBeginPeriod(1)
        mHandler = New TimerEventHandler(AddressOf TimerCallback)
        mTimerId = timeSetEvent(1, 0, mHandler, IntPtr.Zero, 1)
    End Sub

    Private Delegate Sub TimerEventHandler(id As Integer, msg As Integer, user As IntPtr, dw1 As Integer, dw2 As Integer)
    Private Sub TimerCallback(id As Integer, msg As Integer, user As IntPtr, dw1 As Integer, dw2 As Integer)
        iCount += 1
    End Sub
End Class

Open in new window

0
Comment
Question by:XK8ER
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 41749734
It's not quite clear what you want or what's required.

You may use stopwatch class to measure time elapsed: https://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch(v=vs.110).aspx
You may measure in milliseconds or in ticks. 1 tick - 100 ns.

Also you may use DateTime.Ticks property to measure times:
https://msdn.microsoft.com/en-us/library/system.datetime.ticks(v=vs.110).aspx
0
 
LVL 1

Author Comment

by:XK8ER
ID: 41749749
using DateTime is not a good suggestion as it has a resolution of about 15ms and also stopwatch is not good unless you use high resolution stopwatch.

the questions here is more of a concern than anything else, so i'm just worried if there are any casualties by using this code in a production environment or if it is a safe call.
0
 
LVL 30

Accepted Solution

by:
anarki_jimbel earned 2000 total points
ID: 41749800
DateTime - correct  : )

I can't really see any problems with using Windows API.

On another hand, my understanding is that if a system supports high performance counters the .net stopwatch should be high resolution stopwatch. Have a look: https://msdn.microsoft.com/en-us/library/windows/desktop/dn553408(v=vs.85).aspx
0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question