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

How to make FileSystemWatcher Wait for Completion

NBB
I have a FileSystemWatcher set and working with the OnFileCreated
event. Only thing is, it launches too quickly!

I'm copying MP3 files into a directory (the directory that the FSW
monitors) and seeing as how the files being copied are somewhat large,
the OnFileCreated event doesn't wait until the actual copying
procedure is finished. Therefore, the stuff I have set to happen at
the OnFileCreated event doesn't run, because the file is completely
written.

Is there a way to force the OnFileCreated event to delay for a set
time before it executes?
BTW-This email address is no longer in service, so please reply here
in the boards.

Thanks!
Nov 18 '05 #1
2 4014
You can wait til the whole thing will be loaded by trying
to open the file every let's say 3 seconds.

Here is one method I have that serves exactly the same
porpose (in win app, though, but it should work just fine
in asp.net as well):

public static void WaitToLoad(string fileName,int
milliseconds)
{
DateTime dtStart = DateTime.Now;
TimeSpan ts = new TimeSpan(0);
while(true)
{
try
{
ts = DateTime.Now.Subtract
(dtStart);
FileStream fs = new FileStream
(fileName, FileMode.Open, FileAccess.Read, FileShare.None);
fs.Close();
return;
}
catch(FileNotFoundException)
{
throw;
}
catch(IOException exc)
{
if(ts.Milliseconds > milliseconds)
throw new FileNotLoaded
(fileName,milliseconds,exc);
Thread.Sleep(1000);
continue;
}
}
}

Enjoy :)

-----Original Message-----
I have a FileSystemWatcher set and working with the OnFileCreatedevent. Only thing is, it launches too quickly!

I'm copying MP3 files into a directory (the directory that the FSWmonitors) and seeing as how the files being copied are somewhat large,the OnFileCreated event doesn't wait until the actual copyingprocedure is finished. Therefore, the stuff I have set to happen atthe OnFileCreated event doesn't run, because the file is completelywritten.

Is there a way to force the OnFileCreated event to delay for a settime before it executes?
BTW-This email address is no longer in service, so please reply herein the boards.

Thanks!
.

Nov 18 '05 #2
NBB
Thanks, Kikoz!

I liked your idea with the try...catch idea using a non-shared
FileStream to check for access to the specified file. It lead me to
some research in the SDK regarding functionality of a try...catch
block that I never knew.

Evidently, you can label the sections of a try...catch block, just
like in an "On Error GoTo x" event call. I don't think the SDK
explicitly says you can add labels to try...catch blocks, but I tried
it, and it works like a charm.

Thanks for the inspiration! You saved the day!

VB Example Code Follows:
===============================

Try
Conversion:
Dim fs As New FileStream(e.FullPath,FileMode.Open,FileAccess.Rea d,FileShare.None)
fs.Close()
Console.WriteLine("File Created: " & e.Name)
Thread.Sleep(2000)
Process.Start("some.exe", "arguments")
Catch ex As System.IO.IOException
Console.WriteLine("There was an IOException error. Retrying in 2
seconds...")
Thread.Sleep(2000)
GoTo Conversion
Finally
Console.WriteLine("Process Completed.")
End Try

===============================


"Kikoz" <an*******@discussions.microsoft.com> wrote in message news:<09****************************@phx.gbl>...
You can wait til the whole thing will be loaded by trying
to open the file every let's say 3 seconds.

Here is one method I have that serves exactly the same
porpose (in win app, though, but it should work just fine
in asp.net as well):

public static void WaitToLoad(string fileName,int
milliseconds)
{
DateTime dtStart = DateTime.Now;
TimeSpan ts = new TimeSpan(0);
while(true)
{
try
{
ts = DateTime.Now.Subtract
(dtStart);
FileStream fs = new FileStream
(fileName, FileMode.Open, FileAccess.Read, FileShare.None);
fs.Close();
return;
}
catch(FileNotFoundException)
{
throw;
}
catch(IOException exc)
{
if(ts.Milliseconds > milliseconds)
throw new FileNotLoaded
(fileName,milliseconds,exc);
Thread.Sleep(1000);
continue;
}
}
}

Enjoy :)

-----Original Message-----
I have a FileSystemWatcher set and working with the

OnFileCreated
event. Only thing is, it launches too quickly!

I'm copying MP3 files into a directory (the directory

that the FSW
monitors) and seeing as how the files being copied are

somewhat large,
the OnFileCreated event doesn't wait until the actual

copying
procedure is finished. Therefore, the stuff I have set

to happen at
the OnFileCreated event doesn't run, because the file is

completely
written.

Is there a way to force the OnFileCreated event to delay

for a set
time before it executes?
BTW-This email address is no longer in service, so please

reply here
in the boards.

Thanks!
.

Nov 18 '05 #3

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...
2
by: Charlie Kunkel | last post by:
I need help. I have a directory I'm watching for creation of .TIF files, whereupon creation, I need to launch a process (command line exe) that converts the TIF file to a postscript file. (using...
1
by: Andreas | last post by:
Hi, we have some problems with the following situation: A remote application A writes files to a directory monitored by a FileSystemWatcher-instance in application B. A creates the file with...
7
by: Allen Anderson | last post by:
I'm trying to figure out a way to catch when a file has been written to a directory. I currently have it where I can catch when the file begins writing, but this isn't helpful as I need to know...
2
by: John Lee | last post by:
Hi, I wrote a small program that copy a list of files to a specified folder one by one, i.e. this app will copy one file into the specified folder and wait until it's consumed (deleted) then...
1
by: Phil396 | last post by:
I have a windows service that uses a filesystemwatcher to wait for files and process them to a database. Sometimes a large group of files will be cut and paste for the filesystemwatcher to...
1
by: TD | last post by:
I'm watching a folder for new files. When the files arrive, I want to wait until it is done being written to and then move it. Every time I move a file into the test folder, it triggers 3 calls...
9
by: Tushar | last post by:
Followup-To: microsoft.public.dotnet.general Does anyone know when is this event raised, is it: 1) When the file is created but may not have been closed 2) When the file is created AND it has...
4
by: Andrus | last post by:
To reporoduce, run the code. Observed: Form is shown Expected: message box should displayed. How to fix ? Andrus.
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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: 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
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...
0
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...

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.