FileSystemWatcher.WaitForChanged returns, but there is still a lock on the file
- by SnOrfus
I have a program that send a document to a pdf printer driver and that driver prints to a particular directory. After the print I want to attach the pdf to an e-mail (MailMessage) and send it off.
Right now, I send the document to the printer (wich spawns a new process) and then call a FileSystemWatcher.WaitForChanged(WaitForChangedResult.Created) but when the object is created, it's still not done "printing" and the pdf printer still has a lock on it, throwing an error when I try to attach that file to an e-mail.
I've considered a plain Thread.Sleep(2000) or whatever, but that's far less than ideal.
I considered putting the attachment code in a try/catch block and looping on failure, but again, that's just bad news.
I can't really think of an elegant solution.