FileSystemWatcher.WaitForChanged returns, but there is still a lock on the file

Posted by SnOrfus on Stack Overflow See other posts from Stack Overflow or by SnOrfus
Published on 2009-02-18T15:21:32Z Indexed on 2010/03/28 22:43 UTC
Read the original article Hit count: 748

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.

© Stack Overflow or respective owner

Related posts about c#

Related posts about filesystemwatcher