Go Back   Velocity Reviews > Newsgroups > ASP Net
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

ASP Net - How to make FileSystemWatcher Wait for Completion

 
Thread Tools Search this Thread
Old 12-29-2003, 09:50 PM   #1
Default How to make FileSystemWatcher Wait for Completion


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!


NBB
  Reply With Quote
Old 12-30-2003, 12:42 AM   #2
Kikoz
 
Posts: n/a
Default How to make FileSystemWatcher Wait for Completion
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!
>.
>



Kikoz
  Reply With Quote
Old 12-30-2003, 03:40 PM   #3
NBB
 
Posts: n/a
Default Re: How to make FileSystemWatcher Wait for Completion
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" <> wrote in message news:<09fe01c3ce6d$bd504f60$>...
> 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!
> >.
> >



NBB
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Make about $60,000 in 6 months (i made 20,000).txt (1/1) mr_cool DVD Video 2 03-29-2008 02:11 AM
MAKE HONEST LEGAL MONEY WITH $6.00 linnea_damerau@hotmail.com DVD Video 0 05-18-2006 10:13 AM
MAKE FAST MONEY LEGALLY!!!!!!!!!!!!!!!!!! will_strip_4_banannas_boys@yahoo.com DVD Video 0 02-02-2006 07:03 PM
MAKE FAST MONEY LEGALLY!!!!!!!!!!!!!!!!!! will_strip_4_banannas_boys@yahoo.com DVD Video 0 02-02-2006 06:04 PM
make a lot of money fast! baseballerwc@yahoo.com DVD Video 0 01-31-2006 07:16 PM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46