C# 如何为读取文件的StreamReader操作设置超时?

C# 如何为读取文件的StreamReader操作设置超时?,c#,C#,如何为读取文件的StreamReader操作设置超时 using System.IO; using System.Diagnostics; ... using (StreamReader str = new StreamReader(@"C:\test.txt")) { string holder; Stopwatch sw = new Stopwatch(); sw.Start(); while ((holder = str.ReadLine()) != n

如何为读取文件的StreamReader操作设置超时

using System.IO;
using System.Diagnostics;

...

using (StreamReader str = new StreamReader(@"C:\test.txt"))
{
    string holder;
    Stopwatch sw = new Stopwatch();
    sw.Start();
    while ((holder = str.ReadLine()) != null)
    {
        if (holder.Contains("some piece of text that I'm looking for"))
        {
            //do something
            break;
        }                    
        if (sw.ElapsedMilliseconds > 3000) { break; }
    }
    sw.Stop();
    str.Close();
}
我尝试了被接受的答案,但是我得到了一个例外:“这个流不支持超时。”

using System.IO;
using System.Diagnostics;

...

using (StreamReader str = new StreamReader(@"C:\test.txt"))
{
    string holder;
    Stopwatch sw = new Stopwatch();
    sw.Start();
    while ((holder = str.ReadLine()) != null)
    {
        if (holder.Contains("some piece of text that I'm looking for"))
        {
            //do something
            break;
        }                    
        if (sw.ElapsedMilliseconds > 3000) { break; }
    }
    sw.Stop();
    str.Close();
}
这是示例代码:

using System.IO;

...

using (StreamReader reader = new StreamReader(@"C:\test.txt"))
{
    reader.BaseStream.ReadTimeout = 1;
    string result = reader.ReadToEnd();
    reader.Close();                
}
using System.IO;
using System.Diagnostics;

...

using (StreamReader str = new StreamReader(@"C:\test.txt"))
{
    string holder;
    Stopwatch sw = new Stopwatch();
    sw.Start();
    while ((holder = str.ReadLine()) != null)
    {
        if (holder.Contains("some piece of text that I'm looking for"))
        {
            //do something
            break;
        }                    
        if (sw.ElapsedMilliseconds > 3000) { break; }
    }
    sw.Stop();
    str.Close();
}
提前谢谢

using System.IO;
using System.Diagnostics;

...

using (StreamReader str = new StreamReader(@"C:\test.txt"))
{
    string holder;
    Stopwatch sw = new Stopwatch();
    sw.Start();
    while ((holder = str.ReadLine()) != null)
    {
        if (holder.Contains("some piece of text that I'm looking for"))
        {
            //do something
            break;
        }                    
        if (sw.ElapsedMilliseconds > 3000) { break; }
    }
    sw.Stop();
    str.Close();
}

非常感谢大家的帮助,特别是@jeroenmoster

这是
FileStream
下的
StreamReader
,它不支持超时。如果你担心超时读取本地文件,你的应用程序可能有糟糕的设计,你可以考虑<代码> Reader,ReadtoDeAsNyCy]()/代码>,然后应用超时等待在<代码>任务中的解决方案,但是这不会取消I/O操作本身。如果您所需要的只是响应能力,并且不介意操作仍在后台继续,那么这就足够了。我知道没有(简单的)方法可以取消.NET中的文件I/O。为什么文件读取操作需要超时?如果您只想跳过需要超过固定时间的文件,您应该避免使用
ReadToEnd
并分块读取该文件。@AlexC:那么调用
.ReadToEnd
是一种不好的开始方法,因为它会将整个文件加载到内存中——在循环中调用
ReadLine
,该循环会检查每个页面上的标志迭代。读取单行不需要任何超时注意事项。然后将其包装在
任务中。运行
并检查循环中的
取消标记
。如果
async
/
wait
太复杂,您可以通过检查
秒表的
.appeased
在循环本身中实现穷人超时。关键是,如果循环的单个迭代很短,那么可以在每个迭代中检查终止条件。
using System.IO;
using System.Diagnostics;

...

using (StreamReader str = new StreamReader(@"C:\test.txt"))
{
    string holder;
    Stopwatch sw = new Stopwatch();
    sw.Start();
    while ((holder = str.ReadLine()) != null)
    {
        if (holder.Contains("some piece of text that I'm looking for"))
        {
            //do something
            break;
        }                    
        if (sw.ElapsedMilliseconds > 3000) { break; }
    }
    sw.Stop();
    str.Close();
}