Posts Tagged c#
Using Timers in a C# Windows Service
Posted by clifgriffin in Technology on November 20th, 2008
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.
MusicSort 0.1
Posted by clifgriffin in Code on October 4th, 2008
Find a bug?
If you believe you have found a bug, please open a ticket here. This will allow me to track the issue as a single issue and others to comment and give feedback.
I’m finding it difficult to discern user error from bug from configuration differences among different installations. All of this will lead to a quicker turn around for reported issues. Isn’t that cool?
About two years ago I decided to organize all of my MP3 and WMA files into a reasonable structure. I had tried letting Windows Media Player do this but it always seemed inconsistent.
This program does it all in a fraction of a second.

The other methods of sorting are disabled simply because I haven’t rewritten the code to support them. I need to rewrite my sorting method to be more flexible but haven’t the time currently.
The software is being released under the BSD license.
The MP3/WMA tag reading portions were not written by me, and unfortunately, I can’t remember who wrote them. But suffice it to say, those portions of the software are not under the BSD license as I have no rights to release them as such.
Download it now:
Click here to download Music Sort 0.1 including Source…
MTA Purge 0.9 Download
Posted by clifgriffin in Code on October 3rd, 2008
Find a bug?
If you believe you have found a bug, please open a ticket here. This will allow me to track the issue as a single issue and others to comment and give feedback.
I’m finding it difficult to discern user error from bug from configuration differences among different installations. All of this will lead to a quicker turn around for reported issues. Isn’t that cool?
Anyone who manages an exchange environment of any size has undoubtedly come across a scenario where they are asked to remove e-mails from their Exchange MTA outbound queue. This may be due to spamming users or, possibly an e-mail accidentally being sent to many users.
Whatever the case, if the e-mail has not been fully processed by the MTA server, you can still remove these e-mails. Unfortunately, Microsoft has not provided any tools to make this easier.
Thread Safe Calls To Windows Forms in Visual Studio 2005
Posted by clifgriffin in Technology on July 9th, 2007
I have been working on an increasingly complex monitoring application at work and recently chose to implement threads. This led to many thread safety issues. At first glance, it appeared I would have to rewrite every method or every call to a Form component to avoid cross-thread calls.
However, thanks to this genius article, I have made my code thread safe with almost 0 effort.
If only I had found it earlier.
Recent Comments