473,734 Members | 2,693 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4165
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********@nos pam.nospam> wrote in message
news:Ol******** ******@TK2MSFTN GP02.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(objec t source, FileSystemEvent Args 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.Re adNextRecord())

{. . .}

}

if(File.Exists( fileName))

{

File.Delete(fil eName);

}

}

}

-----

"William Stacey [MVP]" <wi************ @gmail.com> wrote in message
news:eM******** ******@TK2MSFTN GP02.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********@nos pam.nospam> wrote in message
news:Ol******** ******@TK2MSFTN GP02.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(objec t source, FileSystemEvent Args 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.ReadToE nd();
Console.WriteLi ne(temp);
}
}
catch(IOExcepti on ex)
{
System.Threadin g.Thread.Sleep( 100);
}
}
if(File.Exists( fileName))
{
File.Delete(fil eName);
}
}
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 FileSystemWatch er 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.m icrosoft.com> wrote in message
news:9M******** *****@TK2MSFTNG XA01.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(objec t source, FileSystemEvent Args 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.ReadToE nd();
Console.WriteLi ne(temp);
}
}
catch(IOExcepti on ex)
{
System.Threadin g.Thread.Sleep( 100);
}
}
if(File.Exists( fileName))
{
File.Delete(fil eName);
}
}
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 FileSystemWatch er 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
9477
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 file is completely written. I understand there is nothing "exactly" like that due to how the OS filesystem works, so I am looking for a workaround. I am currently attempting to open the "new" file in
3
14306
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 a working thread for processing the file, created a new FileSystemWatcher (as he said for real time processing), and then called the join method for the first thread. I can't really see the sence in this. Aren't the events of the...
10
1708
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. if there are other noobs here like me feel free to use my code if it's of any use :) I've set a breakpoint at doWorkOnChangedFile but notinh happens... i'm flooding my log like hell.
2
5208
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 records into our sql server filewatcher app deletes the file. I am trying to figure out the best method of doing this, so I am adding code bit by bit. Currently I have this:
15
2609
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, the main thread waits. At the completion of each subthread, the mainthread checks all 12 thread objects to see if they are done. If they are, raise an event that says we're done. So, it's kinda like this: ProcessThread - Creates a ProcessObject
3
2284
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 seperate objects which we can and must control. On these com objects I added the events via AddHandler. This is working great, I can see that my threads are raising events through the com object.
14
2840
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 every weird way that comes to my mind. Particularly I'm struggling to find a way to use events between threads. In particular I'd like to know how to rise and event from a thread and have it received from the delegate in another thread (I mean the...
4
2052
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 status of the download changes. Some of the events are used to tell the next file to download while others manager other resources. However, on occasion, one event will
0
935
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 inside the form class. To make things easier I simply pass my objects and threads by reference to the form, like this I can handle their events easily with the "AddHandler". For example, if I raise the event from my thread and in the form I...
0
8946
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8776
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9310
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8186
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6735
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6031
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3261
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.