none
nanoseconds per second?

    Question

  • Hi,

    I have the following code snippet.....

    Dim StartTime As DateTime = Now()
    Dim EndTime As DateTime = Now()
    Dim One_Second As Long = ???????   '1 - second in nanoseconds
    While (EndTime.Ticks - StartTime.Ticks) / One_Second <= 2.5  'wait for 2.5 seconds
          Application.DoEvents()
          EndTime = Now()
    End While

    What should the correct value be for the number of nanoseconds per 1 second in time to properly determine the number of seconds in time that have passed?  I've played with various values and the closes one I've had success with is "10,000,000". 

    I chose this routine because it can give finer values less than 500 milliseconds like 0.25 seconds   Any other suggestions and code improvements are appreciated.

    TIA

     

    Tuesday, July 11, 2006 4:13 PM

Answers

  • OK I Looked at this code and was concerned about a couple of items here.

    1. is that if you are expecting a degree of accuracy down to a very small period of time - then I think you might be disappointed as at the precision, as although there the ticks may return a value representing down to the nanosecond.   The processor is doing other things at the time - imaging in that loop that you call the application.doevents which causes it to process outstanding windows requests which lets say take a second.     Then your not going to be accurate down to the any level of precision close to a nanosecond at that point.   Your limited by how long it takes to process the windows requests.     This sort of use of the application.doevents can absolutely kill application performance as can loop an amazing number of times and this short period and is constantly checking on each iteration.

    2. is the idea of running around a loop constantly checking the time - albiet doing a doevents to try and keep the UI active.   If so then you looking at a good candidate for using a timer and event driven model.

    If you are trying to in essence build a 2.5 second delay - then why not use a timer control to implement a solution - the following is a real simple example of starting a timer with a interval and then the method is called when in x number of seconds.   No looping and having to use doevents to try to keep UI active.

    Example

    Public Class Form1
        Dim t As New System.Timers.Timer


        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            AddHandler t.Elapsed, AddressOf TimerFired  '//Add the event handler
        End Sub

        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            t.Interval = 2500
            t.Start() '//In 2.5 sec the timer event will fire.
        End Sub


        Public Sub TimerFired(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs)
            MethodToCall()

            t.Stop() '//Stop the timer now the event has fired
        End Sub

        Sub MethodToCall()
            'Do stuff Here
            MsgBox("2.5 seconds")

        End Sub

    End Class

    Wednesday, July 12, 2006 12:12 AM

All replies

  • OK I Looked at this code and was concerned about a couple of items here.

    1. is that if you are expecting a degree of accuracy down to a very small period of time - then I think you might be disappointed as at the precision, as although there the ticks may return a value representing down to the nanosecond.   The processor is doing other things at the time - imaging in that loop that you call the application.doevents which causes it to process outstanding windows requests which lets say take a second.     Then your not going to be accurate down to the any level of precision close to a nanosecond at that point.   Your limited by how long it takes to process the windows requests.     This sort of use of the application.doevents can absolutely kill application performance as can loop an amazing number of times and this short period and is constantly checking on each iteration.

    2. is the idea of running around a loop constantly checking the time - albiet doing a doevents to try and keep the UI active.   If so then you looking at a good candidate for using a timer and event driven model.

    If you are trying to in essence build a 2.5 second delay - then why not use a timer control to implement a solution - the following is a real simple example of starting a timer with a interval and then the method is called when in x number of seconds.   No looping and having to use doevents to try to keep UI active.

    Example

    Public Class Form1
        Dim t As New System.Timers.Timer


        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            AddHandler t.Elapsed, AddressOf TimerFired  '//Add the event handler
        End Sub

        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            t.Interval = 2500
            t.Start() '//In 2.5 sec the timer event will fire.
        End Sub


        Public Sub TimerFired(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs)
            MethodToCall()

            t.Stop() '//Stop the timer now the event has fired
        End Sub

        Sub MethodToCall()
            'Do stuff Here
            MsgBox("2.5 seconds")

        End Sub

    End Class

    Wednesday, July 12, 2006 12:12 AM
  • What I'm attempting is a way to single-thread database access within my application while multiple events are triggered at various times and to avoid the infamous "concurrent access - cannot have more than one db access occurring at the same time".  I'm using OLDBE as my db application suite accessing a MS Access db.

    I have a windows form that has an online application form used for user interaction and in the background various events that can fire off at different times of the day and when I developed this I was occassionally receiving the "concurrent access" problem and the windows message box would pop up indicating the error.  I did some research to see if I could do locking, mutex, a semaphore and other items and was unable to find a good method with my limited knowledge of VB.Net 2003 and learning while on the job.

    What I'm doing is around each db sql transaction I'm setting a boolean flag that a transaction is starting and when the transaction is done the flag is reset to false.  The code snippet I included is basically a timer to wait for the db transaction to finish before moving forward to do a db transaction in the current event.  This may not be the proper way to do it but this was the easiest I could come up with and get it going.  I tried a central timer to control this and found that because other routines/events were also calling the pausing routine some of them were stepping on each other's toes and causing the application to get into a tight loop and hang.  I also don't want to create a bunch of timers, one for each point prior to a db transaction as that may be hard to follow in the code but if that is the best way then I'll go back and revisit it. 

    If you have other suggestions let me know and I'll be happy to review and try out.  Also I don't care much about the exact nanosecond amount, just an approximation that will simulate pausing for x number of seconds.

    TIA!!

    Friday, July 14, 2006 5:03 AM