Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
public static void Main(string[] args)         
        {             // Retry three times on failure to open file.             
            int retryCount = -1;            
            String line = null;             
            do              
            {                 
                try                  
                {                     
                    StreamReader reader = new StreamReader("Test.txt");                     
                    line = reader.ReadLine();                     
                    reader.Close();                 
                }                  
                catch (IOException)                  
                {                     
                    // Wait a little bit.                     
                    Thread.Sleep(300);                      
                    if (retryCount == -1) {                         
                        // Start the count at three if this is the first occurrence.                         
                        retryCount = 3;                     
                    } 
                    else 
                    {                         
                        // Okay, looks like somebody has tried before.                         
                        retryCount--;                     
                    }                 
                }             
            }              
            while (retryCount > 0);        
 }
Posted
Updated 31-Aug-14 18:44pm
v2

Why would somebody do that for you?

If you have written this code, you are the best person to debug and find out the issue. How we would know the problem without even getting what is the requirement?

Here we try to answer and solve specific problems people face during programming. :)
 
Share this answer
 
Comments
Member 11047851 30-Aug-14 23:57pm    
Thanks Tadit
Welcome. :)
Sorry, this code is impossibly bad. Why doing all that stuff. Every time you say Thread.Sleep(300); especially with hard-coded "300", the question would be: "why not 310, why not 200?". If fact, it all makes no sense at all.
C#
string fileName = //... never a hard-coded file name

//...

using (StreamReader reader = new StreamReader(fileName)) {
   string someData = reader.ReadLine();
   // ... whatever you need to read
} // here, reader.Dispose is automatically called
// read about "using" statement

And nothing else. There is no need to retry. Instead, do some effort to work with the files not touched by other processes. But it the file is opened by some other process, retrying is just silly. Instead, stop trying immediately.

Don't use try-catch on too local level. Do it when you really know what to do with the exception (I call it "competency points"), often only on the very top stack frame of the thread. Exception have been invited to isolate exception handling, not to mix them with code.

—SA
 
Share this answer
 
Comments
Member 11047851 31-Aug-14 3:04am    
Thanks Sergey. This question was asked in the interview and i couldn't figure it out.
C#
public static void Main(string[] args)
{
// Retry three times on failure to open file.
int retryCount = -1;
String line = null;
do
   {
     try
         {
              StreamReader reader = new StreamReader("Test.txt");
              line = reader.ReadLine();
              reader.Close();
           }
      catch (IOException)
           {
          // Wait a little bit.
          Thread.Sleep(300);
     if (retryCount == -1)
     {
          // Start the count at three if this is the first occurrence.
          retryCount = 3;
     }
     else
    {
        // Okay, looks like somebody has tried before.
        retryCount--;
    }

         }
  }
while (retryCount > 0);
  }


reader.Close();
new StreamReader(@"c:\Test.txt");
is loop and hangover your pc
 
Share this answer
 
Comments
Member 11047851 31-Aug-14 11:00am    
but retryCount <0 if no error and max retryCount=3 if there is error.
Condition is controlling the loop how can it hangover PC. Am I missing something here.
Please explain.
Member 11047851 wrote:
Thanks Sergey. This question was asked in the interview and i couldn't figure it out.
As to the interview preparation, see also my past answer:
SSRS interview questions and answers[^].

—SA
 
Share this answer
 
1. Instead of using reader.close() use "using" block. Read more at http://msdn.microsoft.com/en-IN/library/yh598w02.aspx[^]

2. If you are NOT using the "using" block, close the connection in finally block. Like:
C#
StreamReader reader = new StreamReader("Test.txt");
            try
            {
                //StreamReader reader = new StreamReader("Test.txt");
                var line = reader.ReadLine();
            }
            catch (IOException)
            {
            }
            finally
            {
                reader.Close();
            } 

Read more at http://msdn.microsoft.com/en-IN/library/zwc8s4fz.aspx[^]

3. Avoid using Thread.Sleep() because it's harmful. Read at http://stackoverflow.com/questions/8815895/why-is-thread-sleep-so-harmful[^]


Apart from using "using" block and "Thread.Sleep(300)" problems, as the comment says it should try 3 times after the IO read fails. But actually it runs 4 times. I think this might be a point here as it is used in a do...while loop.

Hope it helps.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900