Csharp/CSharp Tutorial/File Directory Stream/Asynchronous Input Output
Содержание
Async File Stream
<source lang="csharp">using System; using System.Collections.Generic; using System.Text; using System.Threading; using System.IO; class Program {
static void Main(string[] args) { Console.WriteLine("Main thread started. ThreadID = {0}", Thread.CurrentThread.GetHashCode()); FileStream fs = new FileStream("logfile.txt", FileMode.Append, FileAccess.Write, FileShare.None, 4096, true); string msg = "this is a test"; byte[] buffer = Encoding.ASCII.GetBytes(msg); fs.BeginWrite(buffer, 0, buffer.Length, new AsyncCallback(WriteDone), fs); } private static void WriteDone(IAsyncResult ar) { Console.WriteLine("AsyncCallback method on ThreadID = {0}", Thread.CurrentThread.GetHashCode()); Stream s = (Stream)ar.AsyncState; s.EndWrite(ar); s.Close(); }
}</source>
Async FileStream demo
<source lang="csharp">using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Threading;
class Program { static void Main(string[] args) { FileStream fs = new FileStream("logfile.txt", FileMode.Append,FileAccess.Write, FileShare.None, 4096, true); string msg = "this is a test"; byte[] buffer = Encoding.ASCII.GetBytes(msg); fs.BeginWrite(buffer, 0, buffer.Length,new AsyncCallback(WriteDone), fs); } private static void WriteDone(IAsyncResult ar) { Stream s = (Stream)ar.AsyncState; s.EndWrite(ar); s.Close(); } }</source>
Asynchronous I/O
<source lang="csharp">using System; using System.IO; using System.Windows.Forms; class MainClass {
private static FileStream inStream; static void OnComplete(IAsyncResult asyncResult) { int bytesRead = inStream.EndRead(asyncResult); Console.Write(bytesRead); } public static void Main() { inStream = new FileStream("C:\\text.txt", FileMode.Open, FileAccess.Read, FileShare.None, 2048, true); AsyncCallback acb = new AsyncCallback(OnComplete); byte[] buf = new byte[50]; inStream.BeginRead(buf, 0, 50, acb, null); for(int i=0; i<10; i++) Console.WriteLine(i); Console.WriteLine("Press Enter to exit"); int resp = Console.Read(); }
}</source>
Asynchronous I/O, some blocking on the main thread
<source lang="csharp">using System; using System.Collections.Generic; using System.Collections.Specialized; using System.IO; using System.IO.rupression; using System.Net; using System.Net.Mail; using System.Runtime.InteropServices; using System.Text; public class MainClass {
public static void Main() { using (Stream s = new FileStream("c:\\test.txt", FileMode.Open)) { byte[] buffer = new byte[4096]; int bytesRead; do { IAsyncResult ar = s.BeginRead(buffer, 0, buffer.Length, null, null); bytesRead = s.EndRead(ar); } while (bytesRead == buffer.Length); } }
}</source>
Asynchronous IO with AsyncCallback
<source lang="csharp">using System; using System.Collections.Generic; using System.IO; using System.Text;
public class MainClass { static Stream inputStream; static AsyncCallback myCallBack; static byte[] buffer; static int BufferSize = 256; public static void Main() { inputStream = File.OpenRead("AskTim.txt" ); buffer = new byte[BufferSize]; myCallBack = new AsyncCallback( OnCompletedRead ); inputStream.BeginRead( buffer, // holds the results 0, // offset buffer.Length, // (BufferSize) myCallBack, // call back delegate null ); // local state object } static void OnCompletedRead( IAsyncResult asyncResult ) { int bytesRead = inputStream.EndRead( asyncResult ); if ( bytesRead > 0 ){ String s = Encoding.ASCII.GetString( buffer, 0, bytesRead ); Console.WriteLine( s ); inputStream.BeginRead(buffer, 0, buffer.Length, myCallBack, null ); } } }</source>