Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Filesystemwatcher synchronous
Message
De
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:
01422242
Vues:
34
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;
>}
>


Charles Hankey

Though a good deal is too strange to be believed, nothing is too strange to have happened.
- Thomas Hardy

Half the harm that is done in this world is due to people who want to feel important. They don't mean to do harm-- but the harm does not interest them. Or they do not see it, or they justify it because they are absorbed in the endless struggle to think well of themselves.

-- T. S. Eliot
Democracy is two wolves and a sheep voting on what to have for lunch.
Liberty is a well-armed sheep contesting the vote.
- Ben Franklin

Pardon him, Theodotus. He is a barbarian, and thinks that the customs of his tribe and island are the laws of nature.
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform