473,387 Members | 1,529 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,387 software developers and data experts.

File IO Race!!

Hi All, Here's a situation, I am trying to process some large Files via a
Win Svc using FileMonitor. During the test process I tried copy and pasting
a file in a folder and got an error saying "Process can't access the file
foo.txt as it is being used by another process". I believe the Copy
operation hasn't finished yet though my WinSvc kicked in.....since it got a
FileChange Notification...
How can I avoid the Race Condition?

TIA
Nov 16 '05 #1
9 1934
Hi, Vai2000

please post complete code snippet demonstrating the problem. Otherwise,
chances are you will get generic answers. Like mine:
- could be that you don't close file when finish copy
- could be that you try to access file while it is not yet ready

First one is problem, second one you can avoid by simply waiting for file to
become available

HTH
Alex

"Vai2000" <no****@microsoft.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Hi All, Here's a situation, I am trying to process some large Files via a
Win Svc using FileMonitor. During the test process I tried copy and pasting a file in a folder and got an error saying "Process can't access the file
foo.txt as it is being used by another process". I believe the Copy
operation hasn't finished yet though my WinSvc kicked in.....since it got a FileChange Notification...
How can I avoid the Race Condition?

TIA

Nov 16 '05 #2
Unfortunately my module is just too large to post out here.... but here's
the scoop
I call a Delegate on FileCreate event.
FileMonitor.Created += new
System.IO.FileSystemEventHandler(this.FileMonitor_ Changed);
Now if you have a large file, the copy operation might not be completed but
the Delegates gets invoked immediately as a file gets created!!! Then the
function in the Delegate tries to access the file... and it throws
exception!!!
How do I know when the file has been completely created so that my worker
classes can access it ?

I was trying to do something like this

retry:
try
{
OperateOnFile(); // FileStram freader = new FileStream(this.fileName,
FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
}
catch(Exception e)
{
Thread.Sleep(15000);
goto retry;
}

But still no Luck!!!
TIA
"AlexS" <sa***********@SPAMsympaticoPLEASE.ca> wrote in message
news:uJ**************@TK2MSFTNGP11.phx.gbl...
Hi, Vai2000

please post complete code snippet demonstrating the problem. Otherwise,
chances are you will get generic answers. Like mine:
- could be that you don't close file when finish copy
- could be that you try to access file while it is not yet ready

First one is problem, second one you can avoid by simply waiting for file to become available

HTH
Alex

"Vai2000" <no****@microsoft.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Hi All, Here's a situation, I am trying to process some large Files via a Win Svc using FileMonitor. During the test process I tried copy and pasting
a file in a folder and got an error saying "Process can't access the file foo.txt as it is being used by another process". I believe the Copy
operation hasn't finished yet though my WinSvc kicked in.....since it

got a
FileChange Notification...
How can I avoid the Race Condition?

TIA


Nov 16 '05 #3
When your delegate gets called, check to see if the file is available.
If not, set a timer for 5 seconds and try again. Keep setting that timer
until the file becomes available... You're probably thinking: "Hey, Michael
Jackson, I know you're the king of pop but do you really know anything about
computers? How can I tell when the file becomes available?" -- well, I'm
glad you asked. I, Michael Jackson, typically try opening the file for
EXCLUSIVE access (see FileStream). If an exception is thrown, the file
hasn't been fully copied yet.

Michael Jackson

"Vai2000" <no****@microsoft.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Hi All, Here's a situation, I am trying to process some large Files via a
Win Svc using FileMonitor. During the test process I tried copy and pasting a file in a folder and got an error saying "Process can't access the file
foo.txt as it is being used by another process". I believe the Copy
operation hasn't finished yet though my WinSvc kicked in.....since it got a FileChange Notification...
How can I avoid the Race Condition?

TIA

Nov 16 '05 #4
I tried doing that...please look at the snippet.
retry:
try
{
OperateOnFile(); // FileStram freader = new FileStream(this.fileName,
FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
}
catch(Exception e)
{
Thread.Sleep(15000);
goto retry;
}

"Michael Jackson's Nose" <ki*******@Neverland.com> wrote in message
news:O1**************@tk2msftngp13.phx.gbl...
When your delegate gets called, check to see if the file is available.
If not, set a timer for 5 seconds and try again. Keep setting that timer
until the file becomes available... You're probably thinking: "Hey, Michael Jackson, I know you're the king of pop but do you really know anything about computers? How can I tell when the file becomes available?" -- well, I'm
glad you asked. I, Michael Jackson, typically try opening the file for
EXCLUSIVE access (see FileStream). If an exception is thrown, the file
hasn't been fully copied yet.

Michael Jackson

"Vai2000" <no****@microsoft.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Hi All, Here's a situation, I am trying to process some large Files via a Win Svc using FileMonitor. During the test process I tried copy and pasting
a file in a folder and got an error saying "Process can't access the file foo.txt as it is being used by another process". I believe the Copy
operation hasn't finished yet though my WinSvc kicked in.....since it

got a
FileChange Notification...
How can I avoid the Race Condition?

TIA


Nov 16 '05 #5
Hi, Vai2000

-quote-
Common file system operations might raise more than one event. For example,
when a file is moved from one directory to another, several OnChanged and
some OnCreated and OnDeleted events might be raised. Moving a file is a
complex operation that consists of multiple simple operations, therefore
raising multiple events. Likewise, some applications (for example,
anti-virus software) might cause additional file system events that are
detected by FileSystemWatcher
-unquote-

That's what docs are saying. Now, you did not post code which copies file.
Copy it manually and check how long does this take. Your timing should be
close to this. Otherwise - see previous posts.

HTH
Alex

"Vai2000" <no****@microsoft.com> wrote in message
news:uO****************@TK2MSFTNGP10.phx.gbl...
Unfortunately my module is just too large to post out here.... but here's
the scoop
I call a Delegate on FileCreate event.
FileMonitor.Created += new
System.IO.FileSystemEventHandler(this.FileMonitor_ Changed);
Now if you have a large file, the copy operation might not be completed but the Delegates gets invoked immediately as a file gets created!!! Then the
function in the Delegate tries to access the file... and it throws
exception!!!
How do I know when the file has been completely created so that my worker
classes can access it ?

I was trying to do something like this

retry:
try
{
OperateOnFile(); // FileStram freader = new FileStream(this.fileName,
FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
}
catch(Exception e)
{
Thread.Sleep(15000);
goto retry;
}

But still no Luck!!!
TIA
"AlexS" <sa***********@SPAMsympaticoPLEASE.ca> wrote in message
news:uJ**************@TK2MSFTNGP11.phx.gbl...
Hi, Vai2000

please post complete code snippet demonstrating the problem. Otherwise,
chances are you will get generic answers. Like mine:
- could be that you don't close file when finish copy
- could be that you try to access file while it is not yet ready

First one is problem, second one you can avoid by simply waiting for file
to
become available

HTH
Alex

"Vai2000" <no****@microsoft.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Hi All, Here's a situation, I am trying to process some large Files
via a Win Svc using FileMonitor. During the test process I tried copy and

pasting
a file in a folder and got an error saying "Process can't access the file foo.txt as it is being used by another process". I believe the Copy
operation hasn't finished yet though my WinSvc kicked in.....since it

got
a
FileChange Notification...
How can I avoid the Race Condition?

TIA



Nov 16 '05 #6
"Vai2000" <no****@microsoft.com> wrote in message
news:uO****************@TK2MSFTNGP10.phx.gbl...
Unfortunately my module is just too large to post out here.... but here's
the scoop
I call a Delegate on FileCreate event.
FileMonitor.Created += new
System.IO.FileSystemEventHandler(this.FileMonitor_ Changed);
Now if you have a large file, the copy operation might not be completed but the Delegates gets invoked immediately as a file gets created!!! Then the
function in the Delegate tries to access the file... and it throws
exception!!!
How do I know when the file has been completely created so that my worker
classes can access it ?

I was trying to do something like this
Hi,

you should lock the process and unlock it using a AutoResetEvent... There
are numerous samples on the net using file access and an AutoResetEvent...

retry:
try
{
OperateOnFile(); // FileStram freader = new FileStream(this.fileName,
FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
}
catch(Exception e)
{
Thread.Sleep(15000);
this is not such a good idea...
goto retry;
}

But still no Luck!!!
TIA
"AlexS" <sa***********@SPAMsympaticoPLEASE.ca> wrote in message
news:uJ**************@TK2MSFTNGP11.phx.gbl...
Hi, Vai2000

please post complete code snippet demonstrating the problem. Otherwise,
chances are you will get generic answers. Like mine:
- could be that you don't close file when finish copy
- could be that you try to access file while it is not yet ready

First one is problem, second one you can avoid by simply waiting for file
to
become available

HTH
Alex

"Vai2000" <no****@microsoft.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Hi All, Here's a situation, I am trying to process some large Files
via a Win Svc using FileMonitor. During the test process I tried copy and

pasting
a file in a folder and got an error saying "Process can't access the file foo.txt as it is being used by another process". I believe the Copy
operation hasn't finished yet though my WinSvc kicked in.....since it

got
a
FileChange Notification...
How can I avoid the Race Condition?

TIA




Nov 16 '05 #7
"Vai2000" <no****@microsoft.com> wrote in message
news:uO****************@TK2MSFTNGP10.phx.gbl...
Unfortunately my module is just too large to post out here.... but here's
the scoop
I call a Delegate on FileCreate event.
FileMonitor.Created += new
System.IO.FileSystemEventHandler(this.FileMonitor_ Changed);
Now if you have a large file, the copy operation might not be completed but the Delegates gets invoked immediately as a file gets created!!! Then the
function in the Delegate tries to access the file... and it throws
exception!!!
How do I know when the file has been completely created so that my worker
classes can access it ?

I was trying to do something like this


ps: I have dealt with this before. the solution is simple...

Open the file for exclusive access. If an error is thrown, indeed, pauze
1000 (milliseconds), and loop until the exclusive access is possible.
Be cautious, to avoid an eternal loop, in case the file is not released
properly...

Nov 16 '05 #8
"Vai2000" <va*@onebox.com> wrote in message
news:ek**************@TK2MSFTNGP09.phx.gbl...
I tried doing that...please look at the snippet.
retry:
try
{
OperateOnFile(); // FileStram freader = new FileStream(this.fileName,
FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
This is wrong... :)

FileStream(this.fileName, FileMode.Open, FileAccess.Read, FileShare.None);

}
catch(Exception e)
{
Thread.Sleep(15000);
goto retry;
}


Nov 16 '05 #9
File.Copy(src,target,true);

Thats it

"AlexS" <sa***********@SPAMsympaticoPLEASE.ca> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Hi, Vai2000

-quote-
Common file system operations might raise more than one event. For example, when a file is moved from one directory to another, several OnChanged and
some OnCreated and OnDeleted events might be raised. Moving a file is a
complex operation that consists of multiple simple operations, therefore
raising multiple events. Likewise, some applications (for example,
anti-virus software) might cause additional file system events that are
detected by FileSystemWatcher
-unquote-

That's what docs are saying. Now, you did not post code which copies file.
Copy it manually and check how long does this take. Your timing should be
close to this. Otherwise - see previous posts.

HTH
Alex

"Vai2000" <no****@microsoft.com> wrote in message
news:uO****************@TK2MSFTNGP10.phx.gbl...
Unfortunately my module is just too large to post out here.... but here's the scoop
I call a Delegate on FileCreate event.
FileMonitor.Created += new
System.IO.FileSystemEventHandler(this.FileMonitor_ Changed);
Now if you have a large file, the copy operation might not be completed

but
the Delegates gets invoked immediately as a file gets created!!! Then the function in the Delegate tries to access the file... and it throws
exception!!!
How do I know when the file has been completely created so that my worker classes can access it ?

I was trying to do something like this

retry:
try
{
OperateOnFile(); // FileStram freader = new FileStream(this.fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
}
catch(Exception e)
{
Thread.Sleep(15000);
goto retry;
}

But still no Luck!!!
TIA
"AlexS" <sa***********@SPAMsympaticoPLEASE.ca> wrote in message
news:uJ**************@TK2MSFTNGP11.phx.gbl...
Hi, Vai2000

please post complete code snippet demonstrating the problem. Otherwise, chances are you will get generic answers. Like mine:
- could be that you don't close file when finish copy
- could be that you try to access file while it is not yet ready

First one is problem, second one you can avoid by simply waiting for file
to
become available

HTH
Alex

"Vai2000" <no****@microsoft.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
> Hi All, Here's a situation, I am trying to process some large Files

via
a
> Win Svc using FileMonitor. During the test process I tried copy and
pasting
> a file in a folder and got an error saying "Process can't access the

file
> foo.txt as it is being used by another process". I believe the Copy
> operation hasn't finished yet though my WinSvc kicked in.....since

it got
a
> FileChange Notification...
> How can I avoid the Race Condition?
>
> TIA
>
>



Nov 16 '05 #10

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

Similar topics

4
by: deko | last post by:
Is there a way to avoid opening and closing the file "viscount" twice in this code: <?php $file = 'viscount.txt'; if ($counter = @file ($file)) { $line = each($counter); if (!$counter) {...
48
by: Joseph | last post by:
Hi I'm writing a commercial program which must be reliable. It has to do some basic reading and writing to and from files on the hard disk, and also to a floppy. I have foreseen a potential...
1
by: Swami Tota Ram Shankar | last post by:
"Bob Weigel" <dontuwish@nothing.net> wrote in message > > While George Bush, is the epitome of evil, racism, and uncompassionate > > conservatism, Kerry is either deluded, very deluded, or...
2
by: manuelg | last post by:
Here is a code fragment, where I am trying to copy a file, avoiding overwrites and race conditions. The filename gets a '02','03','04' etc appended to the end if a file with that name already...
17
by: Peter Duniho | last post by:
I searched using Google, on the web and in the newsgroups, and found nothing on this topic. Hopefully that means I just don't understand what I'm supposed to be doing here. :) The problem: ...
2
by: aldaris | last post by:
"race N cars are lined up at the starting line of a Formula 1 race. Task Write a program that determines in how many ways the cars can pass the finish line, knowing that there may be more than one...
2
by: antonyliu2002 | last post by:
I do not quite understand the race condition. As I posted a couple of days ago, I create a PDF on the fly in my web application at regular intervals. Users will be able to download the PDF...
7
by: Piotrekk | last post by:
Hi Probability of this phenomena is like 1/200. The only code where I am dealing with file is the following: using (FileStream fs = new FileStream(this.FilePath,...
0
by: moltendorf | last post by:
I've been trying to find a suitable method for preventing race conditions in my own code. Currently I'm using a file and the flock function to prevent code in other threads from executing at the...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.