Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Filesystemwatcher synchronous
Message
De
01/09/2009 18:27:49
Timothy Bryan
Sharpline Consultants
Conroe, Texas, États-Unis
 
 
À
01/09/2009 17:25:33
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Versions des environnements
Environment:
VB 9.0
OS:
Vista
Divers
Thread ID:
01422062
Message ID:
01422251
Vues:
24
The last time I did a file based project I had the same experience. I did not end up using the FileSystem Watcher either. Good luck.
Tim

>Thanks Paul, Al and Viv -
>
>Consensus seemed to be that filewatcher was just going to tell me when the catalog got tickled but in fact what I needed was to test for being able to put a lock on the file. ( that reference Viv gave me this morning to extending the filesystemwatcher really drove that home).
>
>So I ended up with this, and it seems to work quite well :
>
>
>
>        Dim pdfbyte As Byte() = Nothing
>        Me.Creator.cPrintFile(tempfull)
>
>        '-- Establish a time out...just in case
>        Dim timeOut As DateTime = Now.AddSeconds(30)
>        Dim fileExists As Boolean
>        Do While fileExists = False And timeOut < Now()
>
>            fileExists = File.Exists(pdffull)
>            If Not fileExists Then
>                '-- Wait before trying again
>                System.Threading.Thread.Sleep(5000)
>            End If
>        Loop
>
>        '-- Raise an error if file not found
>        If Not fileExists Then
>            Throw New InvalidOperationException("The PDF file was not created.")
>        End If
>
>        '-- Get bytes
>        pdfbyte = File.ReadAllBytes(pdffull)
>
>        '-- Delete the temp files
>        File.Delete(tempfull)
>        File.Delete(pdffull)
>
>        '-- Return byte array 
>
>        Return pdfbyte
>
>
>
>Now I'll fiddle with the timeout/sleep values
>
>In this case the filesystemwatcher seemed to only add another level of complication.
>
>Of course could have also used a try ... catch as suggested.
>
>Thanks to all for getting me going in the right direction
>
>>>I am creating a PDF programmatically. The ActiveX component basically acts as a printer driver and I can't figure out how to know when it is done so I know the file exists for certain so I can read it into a byte array to store.
>>>
>>>I am trying now to do this with system.io.filesystemwatcher but I can't seem to get the syntax right
>>>
>>
>>As far as I know, there isn't a nice easy way to only get notified when the file is closed (which is essentially what you want). Instead, I'd wait until the file has been created, then call out to another method which loops until you can get an exclusive lock on the new file (or times out), ex: (sorry, C# code and may not work as-is)
>>
>>
>>bool IsFileComplete(string fileName, TimeSpan timeout)
>>{
>>	bool complete = false;
>>	DateTime endTime = DateTime.Now.Add(timeout);
>>	
>>	while (endTime > DateTime.Now)
>>	{
>>		try
>>		{
>>			using (FileStream newFile = File.Open(fileName, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
>>			{
>>				complete = true;
>>				break;
>>			}
>>		}
>>		catch (IOException) { }		
>>
>>		System.Threading.Thread.Sleep(100);
>>	} 
>>	
>>	return complete;
>>}
>>
Timothy Bryan
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform