Results 1 to 5 of 5

Thread: auto run code based on time

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2013
    Location
    Newcastle, Australia
    Posts
    158

    auto run code based on time

    i have done some research but found nothing on how to run an event based on pc time.


    I.e
    Say for example a user selects 10pm every day for a file copy to take place how would you set that up in .net

  2. #2
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: auto run code based on time

    Have you thought of using the Task Scheduler?
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  3. #3
    Frenzied Member IanRyder's Avatar
    Join Date
    Jan 2013
    Location
    Healing, UK
    Posts
    1,232

    Re: auto run code based on time

    Hi,

    If this is something which you need to do within your VB project then have a look at this, which is a modification of something I posted a few months ago. Read through the code to understand what is going on and then just replace my sample DateTimes within the lstActivationTimes List with the dates and times you need to check for. You will need to add a ListBox and a Timer to a Form for this demonstration to function correctly:-

    Code:
    Public Class Form1
      Dim lstActivationTimes As New List(Of DateTime)
     
      Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim StartTime As DateTime = Now
        'To demonstrate, set up a list of Times to be evaluated based on
        ' a StartTime. Just change this list to the times that need to be
        'evaluated.
     
        'Be sure to put the times in order of occurance since this 
        'routine does NOT accommodate times that are in the wrong order
        With lstActivationTimes
          .Add(StartTime)
          .Add(StartTime.AddSeconds(2))
          .Add(StartTime.AddSeconds(4))
          .Add(StartTime.AddSeconds(6))
          .Add(StartTime.AddSeconds(8))
          .Add(StartTime.AddSeconds(10))
          .Add(StartTime.AddMinutes(1))
        End With
        SetNextOccuranceOfTimer()
        Timer1.Enabled = True
      End Sub
     
      Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
     
        'Run the code that you need to be Executed Here
        'You can get rid of everything BUT the SetNextOccuranceOfTimer routine
     
        'Show what time the Timer Fired
        ListBox1.Items.Add("Timer Fired @ " & Now.ToLongTimeString)
        SetNextOccuranceOfTimer()
        'Show the next Timer interval
        ListBox1.Items.Add("Next Time Interval is " & (Timer1.Interval / 1000).ToString & " seconds")
      End Sub
     
      Private Sub SetNextOccuranceOfTimer()
        Const millisecondsInSecond As Integer = 1000
        Dim ListTimeUsed As Boolean
        Dim currentTimeOfDay As TimeSpan = Now.TimeOfDay
     
        'search the list of Times to be processed and set the next Timer interval
        'for the Timer control correctly
        For Each lstTime As DateTime In lstActivationTimes
          If lstTime.TimeOfDay > currentTimeOfDay Then
            Timer1.Interval = CInt(Math.Round(lstTime.TimeOfDay.Subtract(currentTimeOfDay).TotalSeconds, 0) * millisecondsInSecond)
            ListTimeUsed = True
            Exit For
          End If
        Next
     
        'if the current time is greater than any given time in the list then
        'we calculate the period between NOW and the First Time available in the List tomorrow
        If Not ListTimeUsed Then
          Dim EndOfDay As DateTime = DateTime.Parse("23:59:59 PM")
          Timer1.Interval = CInt((Math.Round(EndOfDay.TimeOfDay.Subtract(currentTimeOfDay).TotalSeconds, 0) * millisecondsInSecond) + (Math.Round(lstActivationTimes.Item(0).TimeOfDay.TotalSeconds, 0) * millisecondsInSecond))
          ListBox1.Items.Add("Timer Set to the First Time of the Next Day.")
        End If
      End Sub
    End Class
    As a side note, there have been numerous comments on this Forum as to the accuracy of a Timer over long periods of time so you may need to test this example and post back your own experiences of accuracy over long periods of time.

    Hope that helps.

    Cheers,

    Ian

  4. #4
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: auto run code based on time

    Quote Originally Posted by mason84 View Post
    i have done some research but found nothing on how to run an event based on pc time.


    I.e
    Say for example a user selects 10pm every day for a file copy to take place how would you set that up in .net
    Ian brings up a good point. Based on your example of 10PM, do you mean 10:00:00.000, or would 10PM plus or minus a few seconds be good?
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  5. #5
    New Member
    Join Date
    Jan 2013
    Posts
    13

    Re: auto run code based on time

    Why dont you try something like this:

    Code:
    Function Run_OnDateTime(ByVal userDate as Datetime)
    Dim inputdate As Datetime
    
    If date.now>=inputdate then
    ' Whatever you'd like to do goes here
    End if
    
    End Function
    Usage: Run_OnDateTime(combobox1.selecteditem.text)

    Please note that this has not been tested. This is how I'd approach the situation.

    Hope it helps.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width