Service executes twice

  • Thread starter Tom Van den Brandt
  • Start date
T

Tom Van den Brandt

Hi,

I wrote a simple windows service that includes a FileSystemWatcher.
After building and installing the service (installutil) I notice that
the service executes twice. I included a write to the application
eventlog in OnStart ("service has started and is ready to watch the
filesystem") and this results in two entries in the eventlog.

Also, the handler for the FileSystemWatcher seems to be fired twice if
something happens with a file the service is watching.

I checked the machines services and in the task manager but I see only
one instance of the service running.

I'm a newbie at this, so I probably missed something obvious... Could
this be the properties of the service that were not correctly configured?

Any ideas?

Thx,

Tom
 
H

Harry

Tom Van den Brandt said:
Hi,

I wrote a simple windows service that includes a FileSystemWatcher.
After building and installing the service (installutil) I notice that
the service executes twice. I included a write to the application
eventlog in OnStart ("service has started and is ready to watch the
filesystem") and this results in two entries in the eventlog.

Also, the handler for the FileSystemWatcher seems to be fired twice if
something happens with a file the service is watching.

I checked the machines services and in the task manager but I see only
one instance of the service running.

I'm a newbie at this, so I probably missed something obvious... Could
this be the properties of the service that were not correctly configured?

Any ideas?

Thx,

Tom

Tom, often we look at code and miss that what we are trying to accomplish is
often sabotaged by lack of observation or object/control events. Two of the
great debugging tools I have discovered are sleep and alcohol. Without
seeing your code, it is not possible to give advice. I have solved many
problems just by putting the problem to bed for a while, and come back to it
when ones' emotional state has changed. Cheers
 
T

Tom Van den Brandt

Well,

I had some sleep in the meantime...
And this is what I came up with:
I had the code to write to the application event log in OnStart double.
So part of the problem is corrected. Still, the FilesSystemWatcher
seems to fire twice. Maybe I don't understand its mechanics well enough.

This is what I have in OnStart:

Me.SMSFileWatcher.Path = Path
Me.SMSFileWatcher.IncludeSubdirectories = False
Me.SMSFileWatcher.NotifyFilter = IO.NotifyFilters.LastWrite
Me.SMSFileWatcher.Filter = "freebee.jrn"
Me.SMSFileWatcher.EnableRaisingEvents = True
AddHandler SMSFileWatcher.Changed, AddressOf OnChanged

And this in OnChanged:

My.Application.Log.WriteEntry("An SMS has arrived!")
send("An SMS has arrived")

send passes the message to clients on the network...

So, everytime I open the file, change it and save it, I have the "An SMS
has arrived!" message twice in the Application event log.

Am I doing this correctly?

Thx!


Tom


Harry schreef:
 
P

Phill W.

Tom said:
Still, the FilesSystemWatcher seems to fire twice.
Maybe I don't understand its mechanics well enough.

This is what I have in OnStart:

Me.SMSFileWatcher.Path = Path
Me.SMSFileWatcher.IncludeSubdirectories = False
Me.SMSFileWatcher.NotifyFilter = IO.NotifyFilters.LastWrite
Me.SMSFileWatcher.Filter = "freebee.jrn"
Me.SMSFileWatcher.EnableRaisingEvents = True
AddHandler SMSFileWatcher.Changed, AddressOf OnChanged

Did you declare SMSFileWatcher "WithEvents"?

If so, VB will have /automatically/ wired-up the OnChanged Event for you
and your AddHandler statement will simply add /another/ handler to that
event; it won't "undo" or "replace" an existing one.
And this in OnChanged:

I'm not entirely sure why but, whenever I've used the FileSystemWatcher,
I've had to allow for multiple events in rapid succession - you seem to
get duplicate "change" events even if the file only changes once.

HTH,
Phill W.
 
T

Tom Van den Brandt

Phill W. schreef:
Did you declare SMSFileWatcher "WithEvents"?

Even when I comment out the Addhandler line and the EnableRaisingEvents
line (and put the code in OnChanged in
Private Sub SMSFileWatcher_Changed
The code is still executed twice.

I'm thinking that for some reason the filewatcher is firing the event
twice...

Thx for your suggestions!
 
T

Tom Van den Brandt

Tom Van den Brandt schreef:
Also, the handler for the FileSystemWatcher seems to be fired twice if
something happens with a file the service is watching.

Ok, seems I found the reason for the event firing twice. Apparently the
VS documentation states:

"Multiple Created Events Generated for a Single Action
You may notice in certain situations that a single creation event
generates multiple Created events that are handled by your component.
For example, if you use a FileSystemWatcher component to monitor the
creation of new files in a directory, and then test it by using Notepad
to create a file, you may see two Created events generated even though
only a single file was created. This is because Notepad performs
multiple file system actions during the writing process. Notepad writes
to the disk in batches that create the content of the file and then the
file attributes. Other applications may perform in the same manner.
Because FileSystemWatcher monitors the operating system activities, all
events that these applications fire will be picked up.

Note Notepad may also cause other interesting event generations. For
example, if you use the ChangeEventFilter to specify that you want to
watch only for attribute changes, and then you write to a file in the
directory you are watching using Notepad, you will raise an event . This
is because Notepad updates the Archived attribute for the file during
this operation."

So notepad is the problem here, not my code...

I'll have to do some testing on how the FileSystemWatcher reacts on
changes by other applications, especially the one that will be modifying
the file of course...
 

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