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

File in use: How to tell.

I have an application that is sending a file to a location on a network. I
have a windows service that is monitoring that location for new files and
then processing them. Sometimes the service tries to process them before
that are copied over completely. How can I tell if the other application is
done copying the file over?

Thanks

Dan Reber
Nov 15 '05 #1
2 19532
Add the code in the service, which will examine why it fails. If file is in
use service has to wait until it is released and retry attempt to access
file. Of course, you'll need to use also timeouts - you can't wait
indefinitely, right?

HTH
Alex

"Daniel Reber" <no****@nospam.com> wrote in message
news:%2***************@TK2MSFTNGP12.phx.gbl...
I have an application that is sending a file to a location on a network. I have a windows service that is monitoring that location for new files and
then processing them. Sometimes the service tries to process them before
that are copied over completely. How can I tell if the other application is done copying the file over?

Thanks

Dan Reber

Nov 15 '05 #2
private const string RETRY_MAX_TIME_EXCEEDED = "The file \"{0}\" could not
be processed because a timeout occurred while waiting for the file to finish
uploading.";

private const int RETRY_MAX_TIME = 3600000; // 1 hour
private const int RETRY_DELAY = 10000; // 10 seconds

/// <summary>
/// The event handler that is called when a new file has been uploaded to
the
/// watched directory.
/// </summary>
private void FileCreated(object sender, FileSystemEventArgs args)
{
string filename = args.FullPath;
DateTime receivedAt = DateTime.Now;

bool timedOut = false;
bool processed = false;

//
// The event will be raised as soon as the first byte is written to the
file.
// Check to see if the file has completed uploading. If it hasn't, wait
for a
// specified delay and check again.
//
while (!(timedOut || processed))
{
if (FileUploadCompleted(filename))
{
ProcessFile(filename);
processed = true;
}
else
{
TimeSpan timeElapsed = DateTime.Now - receivedAt;

if (timeElapsed.TotalMilliseconds > RETRY_MAX_TIME)
{
timedOut = true;
}
else
{
Thread.Sleep(RETRY_DELAY);
}
}
}

if (timedOut)
{
LogError(String.Format(RETRY_MAX_TIME_EXCEEDED, filename));
}
}

/// <summary>
/// Returns true if the specified file has completed uploading and is ready
for
/// processing.
/// </summary>
private bool FileUploadCompleted(string filename)
{
//
// If the file can be opened for exclusive access it means that the file
// is no longer locked by the FTP server program and has completed
uploading.
//
try
{
using (FileStream inputStream = File.Open(filename, FileMode.Open,
FileAccess.Read,
FileShare.None))
{
return true;
}
}
catch (IOException)
{
return false;
}
}

"Daniel Reber" <no****@nospam.com> wrote in message
news:%2***************@TK2MSFTNGP12.phx.gbl...
I have an application that is sending a file to a location on a network. I have a windows service that is monitoring that location for new files and
then processing them. Sometimes the service tries to process them before
that are copied over completely. How can I tell if the other application is done copying the file over?

Thanks

Dan Reber

Nov 15 '05 #3

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

Similar topics

5
by: H Brown New To It | last post by:
I am very new to Java. How would you go about making Java program that reads in a fixed length text file (myfile.txt) and pass the data into a table in an sql table? The text file data looks...
5
by: simon place | last post by:
is the code below meant to produce rubbish?, i had expected an exception. f=file('readme.txt','w') f.write(' ') f.read() ( PythonWin 2.3 (#46, Jul 29 2003, 18:54:32) on win32. ) I got...
6
by: Russell E. Owen | last post by:
At one time, mixing for x in file and readline was dangerous. For example: for line in file: # read some lines from a file, then break nextline = readline() # bad would not do what a naive...
3
by: Pernell Williams | last post by:
Hi all: I am new to Python, and this is my first post (and it won't be my last!), so HELLO EVERYONE!! I am attempting to use "xreadlines", an outer loop and an inner loop in conjunction with...
5
by: Richard | last post by:
Hi, Can anyone tell me what the difference is between for line in file.readlines( ): and for line in file:
9
by: Hans-Joachim Widmaier | last post by:
Hi all. Handling files is an extremely frequent task in programming, so most programming languages have an abstraction of the basic files offered by the underlying operating system. This is...
11
by: Dorsa | last post by:
HI, Could you please tell me the error in here. I am trying to open an XML file from a link. Response.Clear() Response.Expires = 0 Response.BufferOutput = False Response.ContentType =...
7
by: Shankarjee Krishnamoorthi | last post by:
Hi, I am new to Python. I am trying to do the following inp = open(my_file,'r') for line in inp: # Perform some operations with line if condition something: # Start re reading for that...
13
by: thomas.mertes | last post by:
Hello Recently I discovered some problem. I have some C code which determines how many bytes are available in a file. Later I use this information to malloc a buffer of the correct size before...
3
by: Magdoll | last post by:
I was trying to map various locations in a file to a dictionary. At first I read through the file using a for-loop, but tell() gave back weird results, so I switched to while, then it worked. ...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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.