![]() |
|
|
|||||||
![]() |
ASP Net - How to make FileSystemWatcher Wait for Completion |
|
|
Thread Tools | Search this Thread |
|
|
#1 |
|
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 |
|
|
|
|
#2 |
|
Posts: n/a
|
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 |
|
|
|
#3 |
|
Posts: n/a
|
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 |
|
![]() |
| Thread Tools | Search this Thread |
|
|
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 |