本文介绍了以下代码有什么问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 public static void Main( string [] args) { // 无法打开文件时重试三次。 int retryCount = -1; 字符串 line = null ; 执行 { 尝试 { StreamReader reader = new StreamReader( Test.txt ); line = reader.ReadLine(); reader.Close(); } catch (IOException) { // 等一下。 Thread.Sleep( 300 ); if (retryCount == -1){ // 如果这是第一次出现,则从三开始计数。 retryCount = 3 ; } 其他 { // 好吧,看起来有人曾尝试过。 retryCount--; } } } while (retryCount > 0 ); } 解决方案 为什么有人会这样做? 如果您编写了此代码,那么您就是调试并找出问题的最佳人选。我们如何在没有得到要求的情况下知道问题? 这里我们尝试回答并解决人们在编程过程中遇到的具体问题。 :) 对不起,这段代码不可思议。为什么要做所有这些事情。每当你说 Thread.Sleep(300);特别是硬编码的300,问题是:为什么不是310,为什么不是200?。事实上,一切都没有意义。 string fileName = // ...永远不是硬编码的文件名 // ... 使用(StreamReader) reader = new StreamReader(fileName)){ string someData = reader.ReadLine(); // ...无论您需要阅读什么 } // 此处,自动调用reader.Dispose / / 阅读使用声明 没有别的。无需重试。相反,要努力处理其他进程未触及的文件。但它的文件是由其他一些进程打开的,重试只是愚蠢。相反,请立即停止尝试。 不要在本地级别使用try-catch。当你真正知道如何处理异常(我称之为能力点)时,通常只在线程的最顶层堆栈框架上执行此操作。已邀请异常隔离异常处理,而不是将它们与代码混合。 -SA public static void Main( string [] args) { // 无法打开文件时重试三次。 int retryCount = -1; 字符串 line = null ; 执行 { 尝试 { StreamReader reader = new StreamReader( Test.txt ); line = reader.ReadLine(); reader.Close(); } catch (IOException) { // 等一下。 Thread.Sleep( 300 ); if (retryCount == -1) { // 如果这是第一次出现,则从三开始计数。 retryCount = 3 ; } 其他 { // 好吧,看起来有人曾经尝试过。 retryCount--; } } } while (retryCount > 0 ); } reader.Close(); 新StreamReader(@c:\\ \\ test.txt); 是循环和宿醉你的电脑 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); } 解决方案 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. :)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.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" statementAnd 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.—SApublic 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 这篇关于以下代码有什么问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-10 09:06