Hi,
I am using DispatcherTimer in my WPF application, following is the link from where I learned it

http://msdn.microsoft.com/en-us/library/System.Windows.Threading.DispatcherTimer.aspx#Y1710

Now problem is I cant make it available outside the procedure, and declaring it globally throws error.
Is there any way to make it global?
Or any other way to use timer in WPF?

Dani AI

Generated

Nice find, — the symptom you saw wasn’t the timer itself but variable scope and declaration form. Both of the forms shown in the thread create a DispatcherTimer instance, but whether the variable is usable outside the procedure depends on where and how you declare it. ’s remark about “creating vs assigning” points in the right direction, but the practical fix is to move the variable to class scope (so every method in the class can access it) and then initialize/configure it where it makes sense (constructor or Loaded).

A few concrete clarifications:

  • Dim name = ... uses local type inference (VB infers the type from the right-hand side). In older project templates this requires Option Infer to be enabled; if it’s off you should use an explicit As declaration.
  • Dim name As New ... explicitly declares the variable type and instantiates it immediately.
  • Neither form makes the variable “global” if written inside a procedure — to share the timer across methods declare it at class level.

Practical pattern (declare at class level and wire events):

Private WithEvents _uiTimer As System.Windows.Threading.DispatcherTimer

' Initialize in the constructor or Window_Loaded (set Interval, Start/Stop)
' _uiTimer = New System.Windows.Threading.DispatcherTimer()
' AddHandler _uiTimer.Tick, AddressOf OnTick   ' or use Handles with WithEvents

Private Sub OnTick(sender As Object, e As EventArgs) Handles _uiTimer.Tick
    ' tick logic here
End Sub

Extra tips: stop the timer when the window closes to avoid stray callbacks, and use AddHandler if you prefer not to use WithEvents/Handles. If the work triggered by the tick is CPU-heavy or must not run on the UI thread, use System.Timers.Timer or a background Task and marshal UI updates back to the Dispatcher. Also prefer a clear field name (for example _uiTimer) and keep Option Strict/Option Explicit enabled for safer code.

Recommended Answers

All 2 Replies

Got the solution :)

Dim dispatcherTimer = New Threading.DispatcherTimer()

I changed declaration from above to

 Dim DispatcherTimer1 As New DispatcherTimer()

Can anybody tell me what is difference between these both declaration?
I am using .NET 3.5

 Dim dispatcherTimer = New Threading.DispatcherTimer()

Over here dispatcherTimer doesn't exist and you are setting it equal to a New Threading.DispatcherTimer() where as now

Dim DispatcherTimer1 As New DispatcherTimer()

you are creating it as a new dispatcher timer.

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.