Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Timer ain't "timing" 1

Status
Not open for further replies.

IlyaRabyy

Programmer
Nov 9, 2010
571
US
Colleagues,
Haven't dealt with Timer class "long, long ago in a language far, far away" (apologies, George!)
Wrote a function:

Code:
'===================================================================================================================================
Public Sub WaitNSeconds(Optional ByVal tiSeconds As Int32 = 0)
'===================================================================================================================================
' Purpose       : Waits the given number of seconds.
' Description   : Employs System.Timers.Timer object to count seconds.
' Parameters    : Number of seconds to pause.
' Side effects  : None.
' Notes         : 1. Generic, applies to .NET Framework ver. 1.1, .NET Core 2.0, .NET Standard 2.0 and higher.
'                 2. Silent.
' Revisions     : 2020-08-25 by Ilya – started 1st draft.
'===================================================================================================================================
Dim loTimer As Timer = New Timer()

With loTimer
   .Interval = (tiSeconds * 1000)
   .AutoReset = False
   .Enabled = True
End With

End Sub
'===================================================================================================================================

Here's the test code:

Code:
Dim lcLogStr As String = Now.ToString("yyyy-MM-dd HH:mm:ss") & ": staring to test WaitNSeconds() function" & vbCrLf

For liCnt As Int16 = 1 To 5
   WaitNSeconds(2)
   lcLogStr = lcLogStr & Now.ToString("yyyy-MM-dd HH:mm:ss") & ": " & liCnt.ToString() & " iteration ended" & vbCrLf
Next

Using loSW = New StreamWriter(gcLogFile, False)
   loSW.Write(lcLogStr)
   loSW.Flush()
End Using

End

And here's the contents of that log file:

"2020-08-25 16:37:24: staring to test WaitNSeconds() function
2020-08-25 16:37:24: 1 iteration ended
2020-08-25 16:37:24: 2 iteration ended
2020-08-25 16:37:24: 3 iteration ended
2020-08-25 16:37:24: 4 iteration ended
2020-08-25 16:37:24: 5 iteration ended"

Evidently, the code in WaitNSeconds() ain't working.

What am I doing wrong?
Please advise.
TIA

Regards,

Ilya
 
Not how a Timer is used; it doesn't wrap a sleep method. It raises an event that you can respond to, eg:

Code:
[blue]Imports System.Timers

Public Class Form3
    Dim WithEvents myTimer As Timer
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        myTimer = New Timer
        myTimer.Interval = 5000
        myTimer.Enabled = True
    End Sub

    Private Sub myTimer_Elapsed(sender As Object, e As ElapsedEventArgs) Handles myTimer.Elapsed
        [COLOR=green]' Do your stuff here[/color]
        Debug.Print("tick")
    End Sub
End Class[/blue]

Mind you, if you are running from a form (as I am) then you can use the Timer control ...
 
StrongM said:
if you are running from a form (as I am) then you can use the Timer control

Not a form (t'd be easy, indeed).
It's a console app run by Windows Scheduler.
The goal is to check the presence of certain files in a certain subdir, and it's a function in this app that does that.
This ChkFiles() function is called from Main() sub and returns Boolean flag. If file(s) ain't there - Main() sub is to wait, say, 60 seconds and run this ChkFiles() function again, 3 times.
If files still ain't there - Main() reports it (writes to that gcLogFile) and quits.

I think I'll try to utilize DatTime.Ticks instead...

Regards,

Ilya
 
This

Code:
'===================================================================================================================================
Public Sub WaitNSeconds(Optional ByVal tiSeconds As Int32 = 0)
'===================================================================================================================================
' Purpose       : Waits the given number of seconds.
' Description   : Employs DateTime.Now object to count seconds by multiplying elapsed System's ticks by 10^7.
' Parameters    : Number of seconds to pause.
' Side effects  : None.
' Notes         : 1. Generic, applies to .NET Framework ver. 1.1, .NET Core 2.0, .NET Standard 2.0 and higher.
'                 2. Silent.
' Revisions     : 2020-08-25 by Ilya – started 1st draft.
'===================================================================================================================================
Dim lnTicksBeg As Int64 = DateTime.Now.Ticks, lnTicksEnd As Int64 = lnTicksBeg + (tiSeconds * 10 ^ 7)

Do Until lnTicksBeg < lnTicksEnd
   lnTicksBeg = DateTime.Now.Ticks
Loop

End Sub

ain't working either...

Regards,

Ilya
 
[tt]Do While lnTicksBeg < lnTicksEnd[/tt]

or

[tt]Do Until lnTicksBeg >= lnTicksEnd[/tt]


But you'd be better off using Sleep() here
 
Strongm said:
you'd be better off using Sleep()
Ain't we all, colleague, ain't we all? Using more sleep, I mean! [LOL]

Seriously: this System.Threading Thread.Sleep(n * 1000) did the trick!
Thank you, colleague, [thanks2]!

Issue is resolved, case is closed.

Regards,

Ilya
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top