Results 1 to 4 of 4

Thread: Running timer event from service

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2018
    Posts
    159

    Running timer event from service

    Hi

    I need to run some code to copy some files from one place to another at a specific time.

    The PC this is running on won't have a user logged in at the time, so I understand this requires a windows service.

    I've created this and added a system timer - from what I've researched the regular timer won't work. Test code is below:

    Code:
    Public Class Service1
        Public launchtime As String = "8:55"
        Protected Overrides Sub OnStart(ByVal args() As String)
            ' Add code here to start your service. This method should set things
            ' in motion so your service can do its work.
            Timer1.Start()
        End Sub
    
        Protected Overrides Sub OnStop()
            ' Add code here to perform any tear-down necessary to stop your service.
        End Sub
    
        Private Sub Timer1_Elapsed(sender As Object, e As Timers.ElapsedEventArgs) Handles Timer1.Elapsed
            Dim mytime As DateTime = Nothing
    
            mytime = Convert.ToDateTime(launchtime)
    
            If mytime.Hour = Now.Hour And mytime.Minute = Now.Minute Then
                MsgBox("Hello it is: " & launchtime)
            End If
        End Sub
    End Class


    I've got my startup object set to the name of the service - I assume this is all that's needed to fire the onstart event and begin the timer?

    I've added ServiceProcessInstaller (account set to LocalService) and ServiceInstaller (startup set to Automatic)

    I've installed as a service and I can see it in Windows services, set to Auto. I've also started it from here.

    Nothing is happening at 8:55 though (I just hard coded this in and used a messagebox while I'm testing)

    Could anyone suggest what I've overlooked?

    Thanks!

  2. #2
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,040

    Re: Running timer event from service

    Hi,

    see if this helps

    Code:
    Option Strict On
    
    
    Public Class Form4
       
        Private timePeriod As TimeSpan
        Private stopWatch As Stopwatch
    
        Private Sub Form4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'Count down the time for ? hour(s)
            timePeriod = TimeSpan.FromHours(14) 'Set to every 14 Hours
    
            stopWatch = stopWatch.StartNew()
            ListBox1.Items.Add("next Action " & Format(Now.AddHours(14), "dd.MM.yyyy HH:mm:ss"))
    
            Timer1.Start()
    
        End Sub
    
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            'The time remaining is the initial interval less the elapsed time.
            Dim timeRemaining = timePeriod - stopWatch.Elapsed
    
            If timeRemaining <= TimeSpan.Zero Then
                'Time is up.
                Timer1.Stop()
                stopWatch.Stop()
                timeRemaining = TimeSpan.Zero
    
     MsgBox("do Action") 'do your Action here
    
                ListBox1.Items.Add("Action " & Format(Now.AddHours(14), "dd.MM.yyyy HH:mm:ss"))
                stopWatch = stopWatch.StartNew()
                Timer1.Start()
            End If
            Label1.Text = String.Format("{0:00}:{1:00}:{2:00}", _
                                                stopWatch.Elapsed.Hours, _
                                                stopWatch.Elapsed.Minutes, _
                                                stopWatch.Elapsed.Seconds)
    
        End Sub
    
       
    End Class
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Sep 2018
    Posts
    159

    Re: Running timer event from service

    Thanks - I need it to run at a specific time rather then an interval, but I'll check this out.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Running timer event from service

    Quote Originally Posted by Precision999 View Post
    I need it to run at a specific time rather then an interval
    Then you calculate the period between the current time and the event time and set that as the Interval of the Timer.

Tags for this Thread

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