Good morning
A few months ago I wrote an application which sits on one of our servers and is triggered at 5-minute intervals by Windows Task Scheduler. The application interrogates a SQL database which has been designed to automatically generate outgoing emails, and when it's triggered it queries a particular table to get the details of all currently-outstanding emails. For various reasons, I want to do away with this approach and instead build a Windows Service which will be triggered to run every 2 seconds, but I've run into a little problem.
For the moment, I'm starting out with a nice simple service which does nothing more than write to a log file when it starts and stops. It is then supposed to write to a second file each time the timer_tick event is fired but isn't doing that. When I attach the VS2010 debugger to the running process, it appears that the timer_tick event is never being fired.
This is the code that's contained in the OnStart event:
This is the code contained in the OnStop event:Code:Timer1.Interval = 2000 Timer1.Enabled = True Timer1.Start() ' Add code here to start your service. This method should set things ' in motion so your service can do its work. Dim fs As FileStream = New FileStream("C:\Temp\arvatoEmailSenderService_vb.txt", FileMode.OpenOrCreate, FileAccess.Write) Dim m_StreamWriter As StreamWriter = New StreamWriter(fs) With m_StreamWriter .BaseStream.Seek(0, SeekOrigin.End) If Timer1.Enabled Then .WriteLine(DateTime.Now.ToShortDateString + " " + DateTime.Now.ToLongTimeString + " : arvatoEmailSenderService: Service Started. Timer is enabled and will ""tick"" every " & Me.Timer1.Interval & " milliseconds.") Else .WriteLine(DateTime.Now.ToShortDateString + " " + DateTime.Now.ToLongTimeString + " : arvatoEmailSenderService: Service Started. Timer would ""tick"" every " & Me.Timer1.Interval & " milliseconds.") End If .Flush() .Close() End With
This is the code that's contained in the timer's Tick event:Code:If Timer1.Enabled = True Then Dim fs As FileStream = New FileStream("C:\Temp\arvatoEmailSenderService_vb.txt", FileMode.OpenOrCreate, FileAccess.Write) Dim m_StreamWriter As StreamWriter = New StreamWriter(fs) With m_StreamWriter .BaseStream.Seek(0, SeekOrigin.End) .WriteLine(DateTime.Now.ToShortDateString + " " + DateTime.Now.ToLongTimeString + " : arvatoEmailSenderService: Service Stopped") .Flush() .Close() End With End If
As you can see, there's nothing particularly fancy going on in the Tick event. As a test, I've also tried that same code in a Timer on a regular Windows form project and it works exactly as it should.Code:' Add code here to perform any tear-down necessary to stop your service. Dim fs As FileStream = New FileStream("C:\Temp\arvatoEmailSenderService_vb2.txt", FileMode.OpenOrCreate, FileAccess.Write) Dim m_StreamWriter As StreamWriter = New StreamWriter(fs) With m_StreamWriter .BaseStream.Seek(0, SeekOrigin.End) .WriteLine(DateTime.Now.ToShortDateString + " " + DateTime.Now.ToLongTimeString + " : arvatoEmailSenderService: Service Ran") .Flush() .Close() End With
I really hope someone can help - I'm in a team of 5 developers and none of us can figure out why this seemingly simple piece of code isn't doing what it should.
TIA




Reply With Quote
