c# - How to set a timeout for a StreamReader operation that reads a file? -


how set timeout streamreader operation reads file?

i tried accepted answer here i'm getting exception: "timeouts not supported on stream."

this sample code:

using system.io;  ...  using (streamreader reader = new streamreader(@"c:\test.txt")) {     reader.basestream.readtimeout = 1;     string result = reader.readtoend();     reader.close();                 } 

thanks in advance.

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 i'm looking for"))         {             //do             break;         }                             if (sw.elapsedmilliseconds > 3000) { break; }     }     sw.stop();     str.close(); } 

thanks lot everyone's help, , in particular @jeroenmostert


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -