Windows service with a timer

  • Thread starter Thread starter Lloyd Sheen
  • Start date Start date
L

Lloyd Sheen

I have a problem where I cannot read (all the time) a file when caught by
the FileSystemWatcher. What I have done to get past this is create an array
of events (when I receive one I add it to the list). I then enable a timer
which will attempt to process the first one in the list.

The timer code is never executed. There is a handles clause on the timer
and using the debugger I have watched the code for the FileSystemWatcher
execute. Code below:

Private Sub FileSystemWatcher1_Created(ByVal sender As Object, ByVal e As
System.IO.FileSystemEventArgs) Handles FileSystemWatcher1.Created
Dim poWE As New WatcherEvents
poWE.FileName = e.FullPath
poWE.Type = "Create"
maEvents.Add(poWE)
Timer1.Interval = 500
Timer1.Enabled = True

LogChange(e.FullPath, "Creating request for ", 102)
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer1.Tick
LogChange("", "Enter timer routine ", 997)
If maEvents.Count = 0 Then Exit Sub

Dim poWE As WatcherEvents
......

The FileSystemWatcher1_Created executes and the log event happens. The
timer1_tick does not execute. Even when not debugging I should see the
event log at the beginning of the code for that routine but there is
nothing.

Lloyd Sheen
 
For anyone trying this, it seems that the timer which you can drag to the
service designer does not work. I guess it has to be hosted by a form.
Once again the IDE should stop the developer from doing this if it will not
work.

Lloyd Sheen
 
I have a problem where I cannot read (all the time) a file when caught by
the FileSystemWatcher. What I have done to get past this is create an array
of events (when I receive one I add it to the list). I then enable a timer
which will attempt to process the first one in the list.

The FileSystemWatcher1_Created executes and the log event happens. The
timer1_tick does not execute. Even when not debugging I should see the
event log at the beginning of the code for that routine but there is
nothing.

Lloyd Sheen

Lloyd,

You can't use the windows forms timer in a service. You need to use
eiter the System.Threading.Timer or the System.Timers.Timer
 
Use the timer in the components tool box, not the windows forms timer. This timer has an elapsed event which is the same as the form timer tick event. I had this same problem when I first made services with the tick not firing, and then I stumbled upon the components timer and this fixed it
 
Back
Top