Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 224
  • Last Modified:

execute a function based on a time setting

Hi,
I have a simple windows service in vb.net. I need to execute a function at a set time of the day. I have an existing process but its not stable. So say the time is set to 03:00, as soon as it gets to 3 or just over i want to execute a function once for that day, very similar to using the scheduled task manager.

thanks
0
CraigLazar
Asked:
CraigLazar
1 Solution
 
Un-ObtainiumCommented:
Why not use the windows task manager? You can right a simple exe to call from a scheduled task that all it does is call the function you want in your windows service.
0
 
jpaulinoCommented:
You just need to use a timer and define a intervar = 1000 (one minute).
In each timer elapsed event you check if the timer is 3:00 and do the function.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Just make a Thread and put it to sleep for the required amount of time:

        Dim T As New System.Threading.Thread(AddressOf SleepingThread)
        T.Start()

With SleepingThread being:

    Private Sub SleepingThread()
        Dim dt As DateTime = DateTime.Today.AddHours(3)
        If DateTime.Now.Subtract(dt).TotalMilliseconds > 0 Then
            dt = dt.AddDays(1) ' it's past 0300...schedule it for tomorrow
        End If
        System.Threading.Thread.Sleep(dt.Subtract(DateTime.Now).TotalMilliseconds)
        ' it's 0300 hrs...do something:

        ' Execute your job here

    End Sub
0
 
CraigLazarAuthor Commented:
thanks
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now