how trigger an event on specific time of day

pls suggest code

Dani AI

Generated

As noted, the simplest option when the process need not run continuously is the Windows Task Scheduler. As suggested, an in-process timer works when the app must be live. Below is a compact, robust VB.NET pattern that avoids tight polling: compute the next occurrence of the target time, set a one-shot System.Threading.Timer for that interval, run the work on the thread-pool, then reschedule for the next day.

Imports System.Threading

Public Class DailyScheduler
    Private ReadOnly _timeOfDay As TimeSpan
    Private _timer As Timer

    Public Sub New(timeOfDay As TimeSpan)
        _timeOfDay = timeOfDay
    End Sub

    Public Sub Start()
        ScheduleNext()
    End Sub

    Private Sub ScheduleNext()
        Dim now = DateTime.Now
        Dim nextRun = New DateTime(now.Year, now.Month, now.Day, _timeOfDay.Hours, _timeOfDay.Minutes, _timeOfDay.Seconds)
        If nextRun <= now Then nextRun = nextRun.AddDays(1)
        Dim dueMs = CInt((nextRun - now).TotalMilliseconds)
        If _timer Is Nothing Then
            _timer = New Timer(AddressOf TimerCallback, Nothing, dueMs, Timeout.Infinite)
        Else
            _timer.Change(dueMs, Timeout.Infinite)
        End If
    End Sub

    Private Sub TimerCallback(state As Object)
        Try
            DoWork()
        Finally
            ScheduleNext()
        End Try
    End Sub

    Private Sub DoWork()
        ' perform action here; keep short or offload long work to Task.Run
    End Sub

    Public Sub [Stop]()
        If _timer IsNot Nothing Then _timer.Dispose() : _timer = Nothing
    End Sub
End Class

Notes and gotchas: TimerCallback runs on the thread-pool—long work should be offloaded (Task.Run) and exceptions must be caught (ScheduleNext is placed in Finally). For UI apps, marshal back to the UI thread. System.Windows.Forms.Timer is unsuitable for reliable background scheduling; prefer System.Threading.Timer or a dedicated Windows Service. Daylight Saving Time and time-zone issues can shift local-time behavior; use DateTimeOffset/TimeZoneInfo for cross-zone correctness or use Task Scheduler / a scheduler library (e.g., Quartz.NET) for complex rules. For testing, set the target time a few minutes ahead and observe behavior.

Recommended Answers

All 2 Replies

Windows Task Manager

Get it to run your code / app at the time of day you need. Else you will have to design something in the app which would need to be running all the time to trigger at the right time.

You can use the Windows Task Scheduler to run a program at a given time. If you want your app to be running, then perform some action at a given time, you can add a Timer control to your app. Set the timer interval to whatever interval you want to test, then at each timer tick (timer event) you can check if the current time is equal to (or within a given number of seconds of) the trigger time. It's better to check using a deadband rather than an exact time in case your app is delayed at the critical time.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.