470,561 Members | 2,210 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,561 developers. It's quick & easy.

Windows Service File Listener

Hey all. I'm now attributing the problem I'm having to something
incredibly simple, to which I must be overlooking.

I created a Windows Service application that listens for files. The
file paths are in a config file. I then write information out to an
event log so as files are dropped into their location, I receive
information. Lastly, I wish to delete the file(s) once they are dropped
.. This is where I'm getting the awkward behavior. I get the following
exception (sometimes):

"The process cannot access the file "C:\temp\test1.txt" because it is
being used by another process."
The sometimes occurence tells me that I am either having a garbage
collection or threading problem. Here is the code I use to add the file
listener for each file:

foreach(XmlNode XNode in Config.DocumentElement.SelectNodes("folder"))
{
fileSystemWatcher = new System.IO.FileSystemWatcher();

((System.ComponentModel.ISupportInitialize)(this.f ileSystemWatcher)).BeginInit();
fileSystemWatcher.Path =
Path.GetDirectoryName(XNode.Attributes["name"].Value.ToString());
fileSystemWatcher.Filter =
Path.GetFileName(XNode.Attributes["name"].Value.ToString());
this.fileSystemWatcher.EnableRaisingEvents = true;
this.fileSystemWatcher.Created += new
System.IO.FileSystemEventHandler(this.fileSystemWa tcher_Created);

((System.ComponentModel.ISupportInitialize)(this.f ileSystemWatcher)).EndInit();
eventLog1.WriteEntry("Adding File Watcher for " +
XNode.Attributes["name"].Value.ToString());
}

this code does work correctly, as the Created event does fire anytime I
add one of the files specified in the app.config file to the directory.

This is the Created event code:

private void fileSystemWatcher_Created(object sender,
System.IO.FileSystemEventArgs e)
{
eventLog1.WriteEntry(e.FullPath.ToString() + " has been delivered");
try
{
File.Delete(e.FullPath);
}
catch (Exception ex)
{
eventLog1.WriteEntry(ex.Message);
}
}
I got desperate and tried to invoke some garbage collection, but the
behavior did not change:

byte[] bytes = new byte[100*(1<<20)];
for(int i=0; i<bytes.Length; i++)
bytes[i] = 0;
bytes = null;
GC.Collect();
Thread.Sleep(2000);

Anyone out there with any ideas, I'd really appeciate it. Thanks
all!!!!

Nov 17 '05 #1
3 15544
Vinny <vi*******@gmail.com> wrote:
Hey all. I'm now attributing the problem I'm having to something
incredibly simple, to which I must be overlooking.

I created a Windows Service application that listens for files. The
file paths are in a config file. I then write information out to an
event log so as files are dropped into their location, I receive
information. Lastly, I wish to delete the file(s) once they are dropped
. This is where I'm getting the awkward behavior. I get the following
exception (sometimes):


<snip>

The problem is just that you're getting told when the file is being
created, not when whatever's creating it has finished writing to it.

You could loop round for a few times, sleeping between attempts, until
you can delete it.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2
Thanks for the quick response. It turns out that the Changed event as
opposed to the Created event provides the consistency I was looking
for. Also, adding a short duration sleep command (100 ms) allowed the
service (which is multi-threaded) to relinquish control to the worker
threads. Unfortunately, using the Changed event , triggers multiple
times as the file is being written to the directory, which results in
multiple writes to the event log for a single file. Is there any way to
check when a file has completed writing after being placed in a folder?

Nov 17 '05 #3
Hi,

No really, you can do one of several things:
1- If you control the file's generator you can opt to create a "flag" file
with the same name that the output file with a different extension, and
"watch" this flag file instead of the content file itself.
2- use a timespan and ignore the events that occur before the timespan
elapse, this would prevent to process two close events.
Or a combination of both.
Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Vinny" <vi*******@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Thanks for the quick response. It turns out that the Changed event as
opposed to the Created event provides the consistency I was looking
for. Also, adding a short duration sleep command (100 ms) allowed the
service (which is multi-threaded) to relinquish control to the worker
threads. Unfortunately, using the Changed event , triggers multiple
times as the file is being written to the directory, which results in
multiple writes to the event log for a single file. Is there any way to
check when a file has completed writing after being placed in a folder?

Nov 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by marc.gibian | last post: by
4 posts views Thread by cmgarcia17 | last post: by
3 posts views Thread by jwpaco | last post: by
4 posts views Thread by pbd22 | last post: by
1 post views Thread by livre | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.