|
-
Mar 9th, 2012, 09:24 AM
#1
Thread Starter
PowerPoster
[RESOLVED] Waiting for a file that I know is going to get created
I have an interface in my program to a process that creates a PDF file. After the PDF file is created, I want to rename it. But I am too quick because I try to rename it and it doesn't exist yet. So the PDF-creation routine must be returning success to me, but then asynchronously creating the file. That is fine if I can wait for it.
So my problem is I am using FileInfo and the first time I check it for .Exists the file doesn't. How do I check again? Can I refresh the FileInfo structure? Do I have to keep allocating a new one? I want to basically sleep and retry, but how do I get recent stats on the file after I sleep?
Code:
FileInfo theFile = new FileInfo(outputFilePath + oldFileName);
while (bWaitForFile && nbrAttempts < maxAttempts)
{
if (theFile.Exists)
File.Move(outputFilePath + oldFileName, outputFilePath + newFileName);
else {
System.Threading.Thread.Sleep(5000);
++nbrAttempts;
continue;
}
}
But if continue loops to again check theFile.Exists, it will always be false because it's static and has info from the first time.
There are 10 kinds of people in this world. Those who understand binary, and those who don't.
-
Mar 9th, 2012, 09:38 AM
#2
Re: Waiting for a file that I know is going to get created
 Originally Posted by MMock
Can I refresh the FileInfo structure?
It would have taken you about 10 seconds to open the documentation and see that the FileInfo class has a Refresh method. Why is someone with almost 3000 posts STILL not reading the documentation?
That said, I'd be using a FileSystemWatcher so that I didn't even try to access the file until it has been created. That still doesn't guarantee that it will be accessible immediately but at least you won;t waste your time trying to access the file when it doesn't even exist.
-
Mar 9th, 2012, 09:42 AM
#3
Re: Waiting for a file that I know is going to get created
@mmock - I am doing almost the exact same thing - processing PDF's and waiting for them to complete.
What PDF driver did you all decide to use? I'm having a hard time finding one that is both "nice to work with" and also affordable for my clients.
The last two I tried don't even move the PDF into the destination folder until "some seconds" after the application creating the PDF completes - very painful to properly manage in a handshake fashion where I want to rename the PDF to a specific spot once done...
Also - I am actively seeking confirmation elsewhere that thread.sleep is ok in a web service. Does your development team already consider this ok practice??
Last edited by szlamany; Mar 9th, 2012 at 10:14 AM.
-
Mar 9th, 2012, 10:21 AM
#4
Thread Starter
PowerPoster
Re: Waiting for a file that I know is going to get created
Yup - I found the Refresh method then had to run to a meeting while I was trying it, so jmcilhinney got here first with the solution. Thanks for your reply and your alternative suggestion, but I don't want to use a fsw.
Steve - I believe I mentioned before that we use PageFlex. It has an engine called mpower. You pass it a command packet with your data, it reads a template, fills in your data and creates the PDF. I can't help you much with the template part of it as that is the job of our designers here. They use PageFlex Studio. It might be overkill for what you're after but we are heavily into it.
I am sleeping here in a console application. But that is a good point, because I will have similar code in my web service. So I might be back to discuss that with you this afternoon!
There are 10 kinds of people in this world. Those who understand binary, and those who don't.
-
Mar 9th, 2012, 10:33 AM
#5
Thread Starter
PowerPoster
Re: Waiting for a file that I know is going to get created
Refresh() is awesome! Thanks for the help.
There are 10 kinds of people in this world. Those who understand binary, and those who don't.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|