none
[Help] Call a function every 24h RRS feed

  • Question

  • Hello friends,

    I have a function that calls my server to check the license status of my program. But I would like to have some function where every 24h that happen call this function and not every time I open my application.

    How could I do it?
    Wednesday, May 17, 2017 11:08 AM

Answers

  • I think we've both agreed. I see something simpler and more practical. My problem is that I would not know how to develop this.

    This I have thought because I really take so many actions and development I see something more complex. I always say that simple things do great things <:-)

    For example:

    In code, set it when it leaves and check it when it starts:

    Option Strict On
    Option Explicit On
    Option Infer Off
    
    Public Class Form1
        Private Sub Form1_Load(ByVal sender As System.Object, _
                               ByVal e As System.EventArgs) _
                               Handles MyBase.Load
    
            With My.Settings
                If (Now - .LastRunDT).TotalHours > 24 Then
                    ' Now run your method
                End If
            End With
    
        End Sub
    
        Private Sub _
            Form1_FormClosing(sender As Object, _
                              e As System.Windows.Forms.FormClosingEventArgs) _
                              Handles Me.FormClosing
    
            If e.CloseReason = CloseReason.UserClosing Then
                With My.Settings
                    .LastRunDT = Now
                    .Save()
                End With
            End If
    
        End Sub
    End Class


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    • Marked as answer by Symantecc Thursday, May 18, 2017 10:48 AM
    Wednesday, May 17, 2017 8:52 PM

All replies

  • In fact only with a windows service

    Calling every 24 hours is then easy, because you can set a system.windows.timer (not a forms one) which elapse every 24 hours.

    https://code.msdn.microsoft.com/windowsapps/VBWindowsService-3fc2805b

    Don't try any kind of desktop app, it fails always because the user can set it off. 


    Success
    Cor


    Wednesday, May 17, 2017 11:23 AM
  • Put this code in a place that will execute only once like the Load or Shown event

            Dim t As New Threading.Thread(AddressOf CheckLicenseStatus)
            t.IsBackground = True
            t.Start()

    Then add this.  You'll need to add the code that calls the existing function that calls server to check the license status of my program, or move that code to this.

        Private Sub CheckLicenseStatus()
            Dim interval As New TimeSpan(24, 0, 0)
            Dim stpw As Stopwatch = Stopwatch.StartNew
            Do
                If stpw.Elapsed >= interval Then
                    'first
                    stpw.Restart()
    
                    'then function that calls server to check the license status of my program
    
                End If
                Threading.Thread.Sleep(5000) 'five seconds - margin of error
            Loop
        End Sub
    


    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it." - MSDN User JohnWein    Multics - An OS ahead of its time.

    Wednesday, May 17, 2017 12:46 PM
  • I'll throw mine in also: A class with an event:

    Option Strict On Option Explicit On Option Infer Off Public Class Form1 Private WithEvents rE As RecurringEvent Private Sub _ Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles MyBase.Load rE = New RecurringEvent(10, 30) End Sub Private Sub _ Form1_FormClosing(sender As Object, _ e As System.Windows.Forms.FormClosingEventArgs) _ Handles Me.FormClosing If rE IsNot Nothing Then rE.Dispose() End If End Sub Private Sub _ rE_Alarm(sender As Object, _ e As System.EventArgs) _ Handles rE.Alarm ' Invoke your function here... ' To prove that it works, the ' program will halt when it gets ' to the following: Stop End Sub End Class Public Class RecurringEvent Implements IDisposable Private tTimer As Threading.Timer Private _alarmHour As Integer Private _alarmMinute As Integer Public Event Alarm(ByVal sender As Object, _ ByVal e As EventArgs) Protected Overridable Sub OnAlarm() RaiseEvent Alarm(Me, New EventArgs) End Sub Public Sub New() _alarmHour = Now.Hour _alarmMinute = Now.Minute tTimer = New Threading.Timer(AddressOf TickTock, Nothing, 60000, 60000) End Sub Public Sub New(ByVal alarmHour As Integer, _ ByVal alarmMinute As Integer) Dim dt As New DateTime(Today.Year, Today.Month, Today.Day, alarmHour, alarmMinute, 0) _alarmHour = dt.Hour _alarmMinute = dt.Minute tTimer = New Threading.Timer(AddressOf TickTock, Nothing, 0, 60000) End Sub Private Sub TickTock(ByVal state As Object) If Now.Hour = _alarmHour AndAlso _ Now.Minute = _alarmMinute Then OnAlarm() End If End Sub Public Sub Dispose() Implements IDisposable.Dispose If tTimer IsNot Nothing Then tTimer.Dispose() tTimer = Nothing End If End Sub End Class


    It uses a threading timer and it's not "to the second precise". It's [about] to the minute precise but that depends on when it's started. For your purpose though, I think this should work.

    Note that the constructor is overloaded. It initializes the threading timer slightly differently depending on which one you use. You can use it with no parameters and if so, it uses the hour and minute of "now"; otherwise it uses whatever hour and minute you give it:

    rE = New RecurringEvent()
    Also, you see the instance of DateTime and that might be confusing: I'm just using that to qualify that you give it a legit time-of-day; other than that it's not used at all.

    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Wednesday, May 17, 2017 3:31 PM
  • My question is this

    You could do, for example, a My.Setting where you save the current date and if it detects that 24h have called that function or it would be something absurd?

    Thanks 

    Wednesday, May 17, 2017 7:49 PM
  • My question is this

    You could do, for example, a My.Setting where you save the current date and if it detects that 24h have called that function or it would be something absurd?

    Thanks 

    Please be clear who you're talking to.

    *****

    It's not absurd depending on what you're wanting to do. For example, let's say that you create a field in Application Settings like you're talking about, and using the .FormClosing event, you set it to their current "Now" (an instance of DateTime).

    When they next start the program, have it look to see if "Now" minus the one saved (so it's a TimeSpan).TotalHours > 24.

    Make sense? That's a lot better than a constantly running process in my opinion.


    "A problem well stated is a problem half solved.” - Charles F. Kettering



    • Edited by Frank L. Smith Wednesday, May 17, 2017 8:02 PM ...reworded
    Wednesday, May 17, 2017 8:00 PM
  • I think we've both agreed. I see something simpler and more practical. My problem is that I would not know how to develop this.

    This I have thought because I really take so many actions and development I see something more complex. I always say that simple things do great things <:-)

    Wednesday, May 17, 2017 8:28 PM
  • I think we've both agreed. I see something simpler and more practical. My problem is that I would not know how to develop this.

    This I have thought because I really take so many actions and development I see something more complex. I always say that simple things do great things <:-)

    For example:

    In code, set it when it leaves and check it when it starts:

    Option Strict On
    Option Explicit On
    Option Infer Off
    
    Public Class Form1
        Private Sub Form1_Load(ByVal sender As System.Object, _
                               ByVal e As System.EventArgs) _
                               Handles MyBase.Load
    
            With My.Settings
                If (Now - .LastRunDT).TotalHours > 24 Then
                    ' Now run your method
                End If
            End With
    
        End Sub
    
        Private Sub _
            Form1_FormClosing(sender As Object, _
                              e As System.Windows.Forms.FormClosingEventArgs) _
                              Handles Me.FormClosing
    
            If e.CloseReason = CloseReason.UserClosing Then
                With My.Settings
                    .LastRunDT = Now
                    .Save()
                End With
            End If
    
        End Sub
    End Class


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    • Marked as answer by Symantecc Thursday, May 18, 2017 10:48 AM
    Wednesday, May 17, 2017 8:52 PM
  • In code, set it when it leaves and check it when it starts:
    Option Strict On
    Option Explicit On
    Option Infer Off
    
    Public Class Form1
        Private Sub Form1_Load(ByVal sender As System.Object, _
                               ByVal e As System.EventArgs) _
                               Handles MyBase.Load
    
            With My.Settings
                If (Now - .LastRunDT).TotalHours > 24 Then
                    ' Now run your method
                End If
            End With
    
        End Sub
    
        Private Sub _
            Form1_FormClosing(sender As Object, _
                              e As System.Windows.Forms.FormClosingEventArgs) _
                              Handles Me.FormClosing
    
            If e.CloseReason = CloseReason.UserClosing Then
                With My.Settings
                    .LastRunDT = Now
                    .Save()
                End With
            End If
    
        End Sub
    End Class


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    If the user leaves the application running for more than 24hrs, this is not actually checking after a 24hr elapsed window.  This is because the code above is writing the "LastRunDT" as the current date/time when it exits.

    To achieve what the OP wants it should be checking the license status and then immediately writing the current date/time to the setting file.  It should also be running a loop on a background thread and checking again after a further 24h has elapsed, unless the OP is convinced the user won't leave it up and running for days on end (his original post stated that it needed to check every 24hrs which is what makes me think it might be left running for a while).

    Also if this is a license status check... might want to encrypt the date/time otherwise it's being written to an XML file and wouldn't take much for a user to modify it.  A quick batch file to edit the value to the current date & time and then launch the app would easily bypass any license checking.

    Thursday, May 18, 2017 10:02 PM
  • Quanta,

    I don't disagree with what you said at all; I don't agree with the premise that the OP posed, but you see how it evolved.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Thursday, May 18, 2017 10:55 PM