473,406 Members | 2,847 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Events, Threads and FileWatcher

I have threads with event handlers to watch for new files placed in
directories. The handler has a Using { ... } block to process the file which
is then deleted. In testing I can drag/drop FileA into one of the watched
directories and everything works fine. But when I drop FileB into one of the
other directories (or the same one, it doesn't matter), I get a "File in
Use" error. This seems odd since the second test is with a different file
and different directory.

Is there something else I need to do in the event handler to release file
resources other than Using{}? Or is there something else causing this
problem?

Thanks for your help!

Regards,
Raj
Apr 17 '06 #1
7 4145
Can we see the code?

Apr 17 '06 #2
The created event does not mean the file is finished with the copy, only
that its directory entry was created. I suspect the file is still opened
from the file copy process and your callback is trying to open the file that
is still locked on Write. As there is not a Closed event, one solution is
to use a temp dir for the copy and then Ren after the copy/download is
complete. Then just listen for the Rename event and do your thing.

--
William Stacey [MVP]

"Raj Wall" <me********@nospam.nospam> wrote in message
news:Ol**************@TK2MSFTNGP02.phx.gbl...
|I have threads with event handlers to watch for new files placed in
| directories. The handler has a Using { ... } block to process the file
which
| is then deleted. In testing I can drag/drop FileA into one of the watched
| directories and everything works fine. But when I drop FileB into one of
the
| other directories (or the same one, it doesn't matter), I get a "File in
| Use" error. This seems odd since the second test is with a different file
| and different directory.
|
| Is there something else I need to do in the event handler to release file
| resources other than Using{}? Or is there something else causing this
| problem?
|
| Thanks for your help!
|
| Regards,
| Raj
|
|
Apr 17 '06 #3
William, hi,

Thanks for the idea. However, the first file tested is processed completely,
and then deleted properly.

But after checking manually (with a file explorer window) and reviewing the
output to make sure the first one finished, the second file dropped in a
different directory gets the "in use" error.

That is, my "Created" event comes from a manual drag and drop of a test file
into the target directory.

My code is below.

Thanks for your help!

Regards,
Raj
---
code excerpt:
---
private void OnCreated(object source, FileSystemEventArgs e) // event
handler for new file

{

string fileName = e.FullPath;

using (StreamReader sr = new StreamReader(e.FullPath))

{

CsvReader myCSVReader = new CsvReader(sr, false);

while (myCSVReader.ReadNextRecord())

{. . .}

}

if(File.Exists(fileName))

{

File.Delete(fileName);

}

}

}

-----

"William Stacey [MVP]" <wi************@gmail.com> wrote in message
news:eM**************@TK2MSFTNGP02.phx.gbl...
The created event does not mean the file is finished with the copy, only
that its directory entry was created. I suspect the file is still opened
from the file copy process and your callback is trying to open the file
that
is still locked on Write. As there is not a Closed event, one solution is
to use a temp dir for the copy and then Ren after the copy/download is
complete. Then just listen for the Rename event and do your thing.

--
William Stacey [MVP]

"Raj Wall" <me********@nospam.nospam> wrote in message
news:Ol**************@TK2MSFTNGP02.phx.gbl...
|I have threads with event handlers to watch for new files placed in
| directories. The handler has a Using { ... } block to process the file
which
| is then deleted. In testing I can drag/drop FileA into one of the
watched
| directories and everything works fine. But when I drop FileB into one of
the
| other directories (or the same one, it doesn't matter), I get a "File in
| Use" error. This seems odd since the second test is with a different
file
| and different directory.
|
| Is there something else I need to do in the event handler to release
file
| resources other than Using{}? Or is there something else causing this
| problem?
|
| Thanks for your help!
|
| Regards,
| Raj
|
|

Apr 19 '06 #4
Hi Raj,

Thanks for posting!

I tested your code in my side, and I found the error will not occur every
time, it appears randomly. If it is not the same with you, please just feel
free to tell me.

I think this is just because the copy process (or another file system
process) are not synchronized, when the "created event" occurs, the copying
is still running, so if you try to open the file, the "File in use"
exception will generate. In this circumstance, to workaround it, we can
catch this IOException and re-execute the StreamReader statement. In the
sample code snippet below, I used an opened flag to track if the exception
is generated from StreamReader constructor:

private static void OnChanged(object source, FileSystemEventArgs e)
{
string fileName = e.FullPath;
bool opened=false;//add it
while (!opened){
try
{
using (StreamReader sr = new StreamReader(e.FullPath))
{
opened=true; //add it
string temp=sr.ReadToEnd();
Console.WriteLine(temp);
}
}
catch(IOException ex)
{
System.Threading.Thread.Sleep(100);
}
}
if(File.Exists(fileName))
{
File.Delete(fileName);
}
}
This workaround is far from perfect, however, I hope it will help to
workaround your problem. Hope it helps!

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Apr 20 '06 #5
Jeffrey, hi,

Thanks for the quick reply! I will implement your suggestion. Also, I saw in
another newsthread that the events generated by FileSystemWatcher are not
guaranteed to coincide with actual file closure and release by the file
system. The post suggested what you propose, but added a Sleep(1000) after
entering the handler to give file system a chance to "wrap up". Then try
again (with sleep) if IO error occurs. I'll try both. Thanks!

Regards,
Raj
""Jeffrey Tan[MSFT]"" <je***@online.microsoft.com> wrote in message
news:9M*************@TK2MSFTNGXA01.phx.gbl...
Hi Raj,

Thanks for posting!

I tested your code in my side, and I found the error will not occur every
time, it appears randomly. If it is not the same with you, please just
feel
free to tell me.

I think this is just because the copy process (or another file system
process) are not synchronized, when the "created event" occurs, the
copying
is still running, so if you try to open the file, the "File in use"
exception will generate. In this circumstance, to workaround it, we can
catch this IOException and re-execute the StreamReader statement. In the
sample code snippet below, I used an opened flag to track if the exception
is generated from StreamReader constructor:

private static void OnChanged(object source, FileSystemEventArgs e)
{
string fileName = e.FullPath;
bool opened=false;//add it
while (!opened){
try
{
using (StreamReader sr = new StreamReader(e.FullPath))
{
opened=true; //add it
string temp=sr.ReadToEnd();
Console.WriteLine(temp);
}
}
catch(IOException ex)
{
System.Threading.Thread.Sleep(100);
}
}
if(File.Exists(fileName))
{
File.Delete(fileName);
}
}
This workaround is far from perfect, however, I hope it will help to
workaround your problem. Hope it helps!

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.

Apr 20 '06 #6
| Also, I saw in
| another newsthread that the events generated by FileSystemWatcher are not
| guaranteed to coincide with actual file closure and release by the file
| system.

hmm. Is that not what I said?

| The post suggested what you propose, but added a Sleep(1000) after
| entering the handler to give file system a chance to "wrap up".

Depends on the file size. A large file could take longer. But I guess if
you try in a loop upto some max tries, then that would work. If you did the
rename, however, you would not have to guess and could open the file in the
Rename event and be sure to open it, unless another process happens to beat
you to it.
Apr 21 '06 #7
Ok, if you need further help, please feel free to feedback. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Apr 21 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Michael Stanbrook | last post by:
I'm using the FileSystemWatcher to looks for new files arriving in a directory. Files are coming in via FTP and tend to be larger is size (> 1MB at least). I would like to fire an event once the...
3
by: Stampede | last post by:
Hi, I want to use the FileSystemWatcher in a Windows Service. I read an article, where the author created the FileSystemWatcher object in a seperate thread and when the event is fired, he started...
10
by: Kai Thorsrud | last post by:
Hi i'm an event Noob and i'm calling a class from my main module console app like this. I'm sorry if this is a lot of code to read but i can't see the error according to my book. Thanks a LOT. ...
2
by: Roger Twomey | last post by:
I am working on a filewatcher application. The premis is: User uploads an xml file onto the web server the filewatcher app sees the xml file filewatcher app reads the file and inserts...
15
by: Bryce K. Nielsen | last post by:
I have an object that starts a thread to do a "process". One of the steps inside this thread launches 12 other threads via a Delegate.BeginInvoke to process. After these 12 threads are launched,...
3
by: daan | last post by:
Hello, I have a problem and I can't get the solution for it :( I have a com dll, which i imported as a reference. The com object is part of a class which is multithreaded and will create...
14
by: Gotch | last post by:
Hi all, I've recently digged into C# and the whole .Net stuff. Particularly I found the idea of adding Events and Delegates to the C# language very interesting and I'm trying to use them in...
4
by: jehugaleahsa | last post by:
Hello: Is there a way to prevent one event from firing while another event is already being fired? I have a tool that extracts media from web pages and it has multiple events firing when the...
0
by: thiago777 | last post by:
Hi! Im still trying to make my application work in an event-driven way. It had worked so far with my threads, but the problem Im having is only when modifying a GUI component from the event handler...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.