c# - Check number of times if file is ready -


i printing printdocument pdf.i store pdf in ms sql table.i have make sure document "printed" before insert column.i have following code check if file "available":

public static bool isfileready(string sfilename) {     try     {          using (filestream inputstream = file.open(sfilename, filemode.open, fileaccess.read, fileshare.none))          {               if (inputstream.length > 0)               {                   return true;               }               else               {                    return false;               }           }     }     catch (exception)     {           return false;     } } 

i want add sort of upper limit either time takes or number of times checks if file ready. if printer fails thread keep waiting forever. how implement it?

this code exits loop if max retries reached or max time has elapsed:

    private const int max_retries = 100;     private const int max_retry_seconds = 120;     public static bool isfileready(string sfilename)     {         int trynumber = 0;         datetime endtime = datetime.now + new timespan(0, 0, max_retry_seconds);          while (trynumber < max_retries && datetime.now < endtime)         {             try             {                 using (filestream inputstream = file.open(sfilename, filemode.open, fileaccess.read, fileshare.none))                 {                     if (inputstream.length > 0)                     {                         return true;                     }                 }             }             catch (exception)             {                 //swallow exception             }              //slow down looping              system.threading.thread.sleep(500);              trynumber += 1;         }          return false;     } 

Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -