473,657 Members | 2,463 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1948
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****@microso ft.com> wrote in message
news:%2******** ********@TK2MSF TNGP12.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.Cre ated += new
System.IO.FileS ystemEventHandl er(this.FileMon itor_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.ReadW rite);
}
catch(Exception e)
{
Thread.Sleep(15 000);
goto retry;
}

But still no Luck!!!
TIA
"AlexS" <sa***********@ SPAMsympaticoPL EASE.ca> wrote in message
news:uJ******** ******@TK2MSFTN GP11.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****@microso ft.com> wrote in message
news:%2******** ********@TK2MSF TNGP12.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****@microso ft.com> wrote in message
news:%2******** ********@TK2MSF TNGP12.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.ReadW rite);
}
catch(Exception e)
{
Thread.Sleep(15 000);
goto retry;
}

"Michael Jackson's Nose" <ki*******@Neve rland.com> wrote in message
news:O1******** ******@tk2msftn gp13.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****@microso ft.com> wrote in message
news:%2******** ********@TK2MSF TNGP12.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 FileSystemWatch er
-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****@microso ft.com> wrote in message
news:uO******** ********@TK2MSF TNGP10.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.Cre ated += new
System.IO.FileS ystemEventHandl er(this.FileMon itor_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.ReadW rite);
}
catch(Exception e)
{
Thread.Sleep(15 000);
goto retry;
}

But still no Luck!!!
TIA
"AlexS" <sa***********@ SPAMsympaticoPL EASE.ca> wrote in message
news:uJ******** ******@TK2MSFTN GP11.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****@microso ft.com> wrote in message
news:%2******** ********@TK2MSF TNGP12.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****@microso ft.com> wrote in message
news:uO******** ********@TK2MSF TNGP10.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.Cre ated += new
System.IO.FileS ystemEventHandl er(this.FileMon itor_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.ReadW rite);
}
catch(Exception e)
{
Thread.Sleep(15 000);
this is not such a good idea...
goto retry;
}

But still no Luck!!!
TIA
"AlexS" <sa***********@ SPAMsympaticoPL EASE.ca> wrote in message
news:uJ******** ******@TK2MSFTN GP11.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****@microso ft.com> wrote in message
news:%2******** ********@TK2MSF TNGP12.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****@microso ft.com> wrote in message
news:uO******** ********@TK2MSF TNGP10.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.Cre ated += new
System.IO.FileS ystemEventHandl er(this.FileMon itor_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******** ******@TK2MSFTN GP09.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.ReadW rite);
This is wrong... :)

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

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


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

Thats it

"AlexS" <sa***********@ SPAMsympaticoPL EASE.ca> wrote in message
news:%2******** ********@TK2MSF TNGP09.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 FileSystemWatch er
-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****@microso ft.com> wrote in message
news:uO******** ********@TK2MSF TNGP10.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.Cre ated += new
System.IO.FileS ystemEventHandl er(this.FileMon itor_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.ReadW rite);
}
catch(Exception e)
{
Thread.Sleep(15 000);
goto retry;
}

But still no Luck!!!
TIA
"AlexS" <sa***********@ SPAMsympaticoPL EASE.ca> wrote in message
news:uJ******** ******@TK2MSFTN GP11.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****@microso ft.com> wrote in message
news:%2******** ********@TK2MSF TNGP12.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
1796
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) { $counter = 1; } else {
48
8470
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 problem. The program may crash unexpectedly while writing to the file. If so, my program should detect this during startup, and then (during startup) probably delete the data added to the file and redo the writing operation.
1
2664
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 plainly a liar. > As to bush being racist...sorry I dont' see that. You make nasty > presumptions about others which makes you no fun to read... uncompassionate > conservativism...I'd like to take you on a trip where we help people. I'd > give...
2
3646
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 exists. I know the writing of the single space is overkill, but I am surprised I cannot find an example of this floating around the web and newsgroups my understanding of 'os.open' and 'os.fdopen' is minimal ## start fragment
17
8018
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: I am trying to use the SaveFileDialog class to get a filename, which is subsequently opened for writing (write access, read sharing, but using read/write sharing doesn't make the problem go away anyway). Sometimes, on the statement where I...
2
1390
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 car arriving at the same time. Input Data Input file race.in contains a single line which has positive integer N on it. Output Data Output file race.out will contain a single line with the number of ways the cars can pass the finish line....
2
2024
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 file. Suppose, at the time when my application is in the process of generating the PDF file (which takes around 2.5 minutes), a user tries to download it, will this be a problem?
7
1946
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, FileMode.Open,FileAccess.Read)) { try {
0
1830
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 same time. For example: <?php $pointer = fopen ('./thread.lock', 'a+'); flock ($pointer, LOCK_EX);
0
8411
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...
1
8513
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8613
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7351
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
6176
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
5638
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
4173
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...
2
1969
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1732
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.