-2

I have a function that needs to execute every 30 mins. How can I filter it using Today.Minute? Here are the Codes I made.

   Protected Sub mainLoop()
    Dim Counting As Integer = 0
    Try
        While (Not stopping)
            While Counting <> 2

                If Today.Minute >= 0 Then
                    If Counting = 1 Then

                    ElseIf Counting = 0 Or Counting = 2 Then
                        PingServers()
                        Counting = Counting + 1

                ElseIf Today.Minute >= 31 Then
                    If Counting = 1 Then
                        PingServers()
                        Counting = Counting + 1

                    ElseIf Counting = 0 Then

                    End If

                End If

            End While

            If Counting = 2 Then
               DoFunction()
            End If
        End While
    Catch ex As Exception

    End Try

End Sub

Please any one. Help :3

Elphrian
  • 71
  • 1
  • 7
  • 2
    Use the timer class to raise an event ever 30 minutes? https://msdn.microsoft.com/en-us/library/system.timers.timer%28v=vs.110%29.aspx – 5uperdan Dec 18 '15 at 08:42
  • 2
    Use the [windows task scheduler](http://fs5.directupload.net/images/151218/24i5xd56.jpg) which exists for this purpose. – Tim Schmelter Dec 18 '15 at 08:43
  • No. Without using time class and task scheduler :3 – Elphrian Dec 18 '15 at 08:45
  • 3
    Empty catchs are no good. Why don't you want to use the right tools? What do you want instead, waiting for 30 minutes? Or really execute an infinite loop which would be a CPU crash test? – Tim Schmelter Dec 18 '15 at 08:48
  • Is your application running constantly? Keep a module level pair of variables (lastRunTime / thisRunTime) and compare them each time in the loop. Put something in that empty catch block before it jumps up and bites you in the ... – Andrew Mortimer Dec 18 '15 at 08:51
  • This is a Window Service app. The example code is edited, maybe I erased the catch statement there. By the way. Help me to program that. – Elphrian Dec 18 '15 at 08:56
  • The function needs to execute Twice an hour. How can I do that? – Elphrian Dec 18 '15 at 08:58
  • Is there a reason not to use the timer? How about http://stackoverflow.com/questions/503564/how-might-i-schedule-a-c-sharp-windows-service-to-perform-a-task-daily – Andrew Mortimer Dec 18 '15 at 09:05

1 Answers1

0

I guess something like this would do the trick:

Module StartupModule

    Private _timer As Timers.Timer

    Sub Main()
        SetTimer(3000)
        Console.ReadLine()
        KillTimer()
    End Sub

    Private Sub SetTimer(interval As Double)
        _timer = New Timers.Timer(interval)
        AddHandler _timer.Elapsed, AddressOf OnTimedEvent
        _timer.Start()
    End Sub

    Private Sub KillTimer()
        _timer.Stop()
        _timer.Dispose()
    End Sub

    Private Sub OnTimedEvent(source As Object, e As ElapsedEventArgs)
        Console.WriteLine(DateTime.Now)
    End Sub

End Module
shadow
  • 1,883
  • 1
  • 16
  • 24