Показаны сообщения с ярлыком timer. Показать все сообщения
Показаны сообщения с ярлыком timer. Показать все сообщения

пятница, 29 апреля 2011 г.

Using Timers in a C# Windows Service


For the most part, writing a windows service in Visual Studio.net is only marginally different than writing a windows application.  You may have difficulty attempting to implement some components you’ve learned to rely on. One such component is the timer.
In a windows application, you simply drag a Timer from the Toolbox and implement as normal.  Curiously, you can do the same in a service–it just doesn’t work.  This is because the timer in the toolbox is located in System.Windows.Forms. So how do you do it? It’s really quite simple.
First, add the following line to the top of your Windows Service project:
using System.Timers;
Next, add this line to top of your class (the section starting with public partial class..)
Timer timer1= new Timer();
Obviously you may want to pick a more descriptive name…just be consistent. Next we need to create the method that will fire each time the timer reaches 0.  
private void timer1_Elapsed(object sender, EventArgs e)
{
      //Some awesome code!
}
Lastly, we need to bind this method to the timer’s elapsed event and finish implementing the timer.  Find your service’s OnStart() method and add something like:
timer1.Elapsed += new ElapsedEventHandler(timer1_Elapsed);
timer1.Interval = 5000;
timer1.Enabled = true;
timer1.Start();
The first line above tells the service to run timer1_Elapsed everytime the timer1.Elapsed event is raised. The second line sets the timer1′s interval to 5 seconds (1000 milliseconds = 1 second).  Lastly we enable the timer and start it.
You may want to add this to your OnStop() method for completeness:
timer1.Enabled = false;
That’s all there is to it.  This should be all you need to know to implement a timer in your service.

пятница, 22 апреля 2011 г.

Сравнение Timer с DispatcherTimer

Windows.Forms.Timer uses the windows forms message loop to process timer events. It should be used when writing timing events that are being used in Windows Forms applications, and you want the timer to fire on the main UI thread.

DispatcherTimer is the WPF timing mechanism. It should be used when you want to handle timing in a similar manner (although this isn't limited to a single thread - each thread has its own dispatcher) and you're using WPF. It fires the event on the same thread as the Dispatcher.

In general, WPF==DispatcherTimer and Windows Forms==Forms.Timer.

That being said, there is also System.Threading.Timer, which is a timer class that fires on a separate thread. This is good for purely numerical timing, where you're not trying to update the UI, etc.

вторник, 25 мая 2010 г.

Timer vs DispatcherTimer



Windows.Forms.Timer uses the windows forms message loop to process timer events. It should be used when writing timing events that are being used in Windows Forms applications, and you want the timer to fire on the main UI thread.
DispatcherTimer is the WPF timing mechanism. It should be used when you want to handle timing in a similar manner (although this isn't limited to a single thread - each thread has its own dispatcher) and you're using WPF. It fires the event on the same thread as the Dispatcher.


In general, WPF==DispatcherTimer and Windows Forms==Forms.Timer.
That being said, there is also System.Threading.Timer, which is a timer class that fires on a separate thread. This is good for purely numerical timing, where you're not trying to update the UI, etc.