mbrc
mbrc

Reputation: 3973

C# DispatcherTimer in DLL application never triggered

I want to use C# Timer in DLL application

DispatcherTimer dispatcherTimer = new DispatcherTimer();
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
TimeSpan interval = TimeSpan.FromMinutes(5);
dispatcherTimer.Interval = interval;
dispatcherTimer.Start();

but the problem is that is never triggered. Method dispatcherTimer_Tick is never triggered even if time is 5 minutes. There is no error nothing.

Any idea why? I am using .net 4.0

Upvotes: 2

Views: 1760

Answers (2)

Deepak Bhatia
Deepak Bhatia

Reputation: 6276

Use System.Timers.Timer instead,

    System.Timers.Timer myTimer = new System.Timers.Timer(5 * 60 * 1000);            
    myTimer.Elapsed += new ElapsedEventHandler(myTimer_Elapsed);            
    myTimer.Enabled = true;
    myTimer.Start();

This works in all the application type, whether app is web, console, WPF etc and you do not need to worry about the environment or application it is been used.

Upvotes: 3

Thomas Levesque
Thomas Levesque

Reputation: 292725

For DispatcherTimer to work, your app must be running a message pump. If it's a console application or web application, there isn't a message pump, so DispatcherTimer can't work.

DispatcherTimer is designed for use in WPF applications. If you need a timer in an application with no UI, use System.Threading.Timer or System.Timers.Timer.

Upvotes: 1

Related Questions