Hi, I am exploring a timer function which I wants my program function runs every 10 minit interval. Below is my coding:

Dim timer As Timer
   timer = New Timer(600000)
        'timer.Enabled = True
        timer.Interval = 600000
        timer.Start()  
       
       'Do function

    timer.Stop()

Actually I am not familiar with timer function, can someone guide me, please? Thanks in advance.

Dani AI

Generated

raised the core question: run a task every 10 minutes from a non‑UI app. pointed toward the designer Timer (which needs a message loop), and suggested a thread‑based timer. Both directions are useful, but a few practical points are missing and worth calling out.

Several pitfalls to watch for: pick a timer implementation that matches the app type (UI timers need a message pump; service/console apps should use a background timer or a worker loop). Keep a strong reference to the timer object so it is not garbage collected. Prevent overlapping executions by using a simple running flag or by disabling the timer at start of the callback and re‑enabling when finished. Always catch and log exceptions inside the callback—unhandled exceptions on background threads can terminate your process or silently stop future callbacks. For long or unpredictable work, schedule the next run relative to a fixed clock (UTC) to avoid drift.

For a Windows Service, create and start the timer from OnStart and stop/dispose it from OnStop. Do not block OnStart while the work runs; run the work on a background thread or via the timer callback. Ensure any shared resources are accessed in a thread‑safe way and that lifecycle (stop, dispose) is handled cleanly to avoid orphaned threads after service stop.

A modern, simple pattern for console apps and services is a cancellable periodic worker using async/await (requires .NET 4.5+). This avoids many timer quirks and makes shutdown predictable:

Private Async Function RunPeriodicAsync(ct As CancellationToken) As Task
    While Not ct.IsCancellationRequested
        Try
            ' perform work here
        Catch ex As Exception
            ' log error
        End Try
        Try
            Await Task.Delay(TimeSpan.FromMinutes(10), ct)
        Catch ex As TaskCanceledException
            Exit While
        End Try
    End While
End Function

Use the above pattern or a properly referenced timer, add a simple reentrancy guard, and make sure to stop/dispose on shutdown. These steps address the common causes of timers "not working" in console apps and services.

Recommended Answers

All 5 Replies

First of all drag and drop one timer on your application then set Enabled property=True
and set interval="whatever u want in my case 6000" and also take one textbox
And now double click the timer control and write the following code....

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If Timer1.Interval = 6000 Then
            TextBox1.Text = 100
        End If
    End Sub

First of all drag and drop one timer on your application then set Enabled property=True
and set interval="whatever u want in my case 6000" and also take one textbox
And now double click the timer control and write the following code....

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If Timer1.Interval = 6000 Then
            TextBox1.Text = 100
        End If
    End Sub

Hi, I am using console application, and not window form application, sorry for the made you confused.

here is a solution:

There is a timer class specifically for console or rather non ui app. which is SYSTEM.THREADING.TIMER , you will have to use it.

Module Module1

    Sub Main()

        Dim tmr As System.Threading.Timer
        Dim tmrCallBack As New System.Threading.TimerCallback(AddressOf DoFuntion)
        tmr = New System.Threading.Timer(tmrCallBack, Nothing, 0, 1000) 'do function will be called ever 1 sec.
        Console.ReadLine()

    End Sub

    Public Sub DoFuntion(ByVal state As Object)
        Console.WriteLine("Do Function Called")
    End Sub

End Module

If I wants to put the coding above into window service application, does it works?

I thinks it is works, thanks you all:)

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.