Windows Service

R

Richard

I've been looking around and have found plenty of services to run
using a Timer control.
What I want to do is have a FileSystemWatcher to run as a service and
tell me when a new file has been created in a directory.
Here is my code.

Protected Overrides Sub OnStart(ByVal args() As String)
Dim fsWatcher As New FileSystemWatcher("C:\temp")
fsWatcher.Filter = "*"
fsWatcher.NotifyFilter = (NotifyFilters.LastAccess Or
NotifyFilters.LastWrite Or NotifyFilters.FileName)
AddHandler fsWatcher.Created, AddressOf OnChanged
fsWatcher.EnableRaisingEvents = True
End Sub

Private Shared Sub OnChanged(ByVal source As Object, ByVal e As
FileSystemEventArgs)
MsgBox("New sync file in C:\temp")
End Sub

The service runs once but then stops because it has no work to do....
How do I keep this service alive?
Thanks in advance
- Rich
 
P

Phill. W

Richard said:
I've been looking around and have found plenty of services to run
using a Timer control. .. . .
Here is my code.

Protected Overrides Sub OnStart(ByVal args() As String)

That will run when the Service is started. Once.
Private Shared Sub OnChanged(ByVal source As Object, ByVal e As
FileSystemEventArgs)
MsgBox("New sync file in C:\temp")
End Sub

The service runs once but then stops because it has no work to do....
How do I keep this service alive?

So where's the Timer?
Without one (a /Timers/ one, *not* a Windows.Forms one) or a
closed loop [preferably] with calls to Threading.Thread.Sleep() in it,
why would the Service do any more than it's doing now?

HTH,
Phill W.
 
R

Richard

Phill. W said:
Richard said:
I've been looking around and have found plenty of services to run
using a Timer control. . . .
Here is my code.

Protected Overrides Sub OnStart(ByVal args() As String)

That will run when the Service is started. Once.
Private Shared Sub OnChanged(ByVal source As Object, ByVal e As
FileSystemEventArgs)
MsgBox("New sync file in C:\temp")
End Sub

The service runs once but then stops because it has no work to do....
How do I keep this service alive?

So where's the Timer?
Without one (a /Timers/ one, *not* a Windows.Forms one) or a
closed loop [preferably] with calls to Threading.Thread.Sleep() in it,
why would the Service do any more than it's doing now?

HTH,
Phill W.

That is the problem because if I have a timer im just reseting the
Event Handler. Which is kind of pointless. but oh well I guess I will
used some unmanaged way to do this.
Thanks
- Rich
 
S

Stephen Martin

Your fsWatcher is declared at the method level in OnStart. When the OnStart
method completes it goes out of scope and is eventually garbage collected.
So it does little, if any, work. Change your declaration to module level and
it will not go out of scope and will call OnChanged on a thread pool thread
as needed. And no you do not need a timer. (Also, I wouldn't put MsgBox in
the OnChanged method if I were you. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top