wait till printer adds file to a directory
-
Wednesday, February 22, 2012 8:02 AM
Hi,
I am using printer driver to print pdf programmatically. Is there any way for me to know that file is available in the specified directory. I used FileSystemWatcher to keep track of it but it affects performance. I also tried to use Print Job status but creating a file takes some time even after print job is done so that also dint work out. Basically what i want is some event which notifies when pdf file is created in the directory.
Thanks
Mehul
All Replies
-
Friday, February 24, 2012 3:13 AMModerator
Hi MehulJSheth,
Welcome to the MSDN forum.
As far as I know, there isn't such an event which notifies when a file is created in the directory. But I think using FileSystemWatcher to watch for changes in a specified directory is still a good way. As to performance, it depends on many factors. You can also consider other ways such as obtaining directory change notifications (but it used c++). Hope it can help you. If I misunderstood you, please let me know.
Have a nice day.
Bob Shen [MSFT]
MSDN Community Support | Feedback to us
- Edited by Bob ShenMicrosoft Contingent Staff, Moderator Friday, February 24, 2012 3:14 AM
-
Tuesday, February 28, 2012 2:41 PM
Hi Bob,
Thanks for the reply. I also thought of FileSystemWatcher and tried also but as you said there is some performance overhead associated with it. Also in my application it happens that multiple files are added to the same folder and all being pdf and FileSystemWatcher keeps track by file type and not by file name so it might happen that it can give unwanted output.
Let me explain you briefly. Application generated 3 pdf files and all 3 goes to same folder. But these 3 files are generated from 3 different function. I created SystemWatcher in all 3 classes and called 3 different event handler(need to perform different task for different pdf) and some time it works fine but sometime gives improper output. Reason might be because all 3 watcher points to the same directory. Any suggestion on it.
Thanks
Mehul
-
Wednesday, February 29, 2012 12:32 AMModerator
I would personally use one FileSystemWatcher. I think putting 3 in to watch the same folder will just cause more problems.
Yes FileSystemWatcher does have some gotchas. Be sure to google and read them and if they are applicable to you code accordingly.
I would then pass the results that the FileSystemWatcher produces off to a queue and use a thread to delay process (perhaps 30 seconds delayed). This way you can ensure that your code isn't triggering for duplicate events which I understand can happen with FSW. Also if you do it this way, you can better test each part of your code individually.
Generally in situations like this I like to do two things. Split your code up (as outlined above). Work on testing each one individaully. Mocking up unit tests and input as necessary. This takes additional development time up front but can drastically reduce time spent later and result in much more robust and bug free code.
Second, logging can ultimately be your friend. use a good library like log4net (sample of how to use it).
Log everything. You can always remove logging later but logging can be your friend and can help you track down and reproduce bugs.
Beyond that it's hard to say without seeing what's happening.
Hope this helps somewhat.
- Marked As Answer by Bob ShenMicrosoft Contingent Staff, Moderator Monday, March 05, 2012 2:34 AM
-
Wednesday, February 29, 2012 1:14 AMModerator
You could also write your own simple DirectoryWatcher class that periodically examines a directory to see if the number of files in it has changed. I would use the Threading.Timer class to perform the polling because it executes on a different thread and will not consume your UI thread. An alternative would be to use a Form.Timer and invoke an asynch delegate in the Tick event handler.
You could check the folder contents by calling Directory.GetFiles, which could fire an event if the number of returned files changes. This behavior would be similar to a FileWatcher. Be aware that the file may exist for a period of time before it is completely written.
Mark the best replies as answers. "Fooling computers since 1971."
http://thesharpercoder.blogspot.com/
- Edited by Rudedog2MVP, Moderator Wednesday, February 29, 2012 1:16 AM
- Marked As Answer by Bob ShenMicrosoft Contingent Staff, Moderator Monday, March 05, 2012 2:34 AM
-
Friday, March 02, 2012 2:20 AMModerator
Hi Mehul,
How's it going? Do you have any updates about the previous issue?
Bob Shen [MSFT]
MSDN Community Support | Feedback to us
-
Monday, March 26, 2012 2:35 PM
Hi Bob,
Sorry for the delay.. I dint check the forum from long. I couldn't find any other way. FileSystemWatcher was the only way i could think of. I used it and it worked. Though there is some performance overhead as i have to keep track of several types of files but i have to bear with it as i don't think there is any other way out.
Thanks,
Mehul

