Results 1 to 5 of 5

Thread: [RESOLVED] Waiting for a file that I know is going to get created

  1. #1

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    My Mustang GT
    Posts
    4,562

    Resolved [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.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Waiting for a file that I know is going to get created

    Quote Originally Posted by MMock View Post
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    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.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  4. #4

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    My Mustang GT
    Posts
    4,562

    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.

  5. #5

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    My Mustang GT
    Posts
    4,562

    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
  •  



Click Here to Expand Forum to Full Width