
using System;

using System.IO;


public class AsyncHronousIO


...{

FileStream myFileStream;

int lngNumber;static bool IsEnd;

static void Main()


...{

AsyncHronousIO myAsyncHronousIO = new AsyncHronousIO();

myAsyncHronousIO.longProcessWrite();

}

public void longProcessWrite()


...{

File.Delete("TT.txt");

Console.Write("輸入數據大小!");

//為什麼數字大了文件就顯示0字節,裡邊沒有內容?

lngNumber = int.Parse(Console.ReadLine());

byte[] b = new byte[lngNumber];

for (int i = 0; i < lngNumber; i++)b[i]=(byte)i;

Console.WriteLine("寫入大量數據!");

//聲明異步回調類,其實就是啟動了一個新的線程,在線程結束時調用
WriteEnd
AsyncCallback myAsyncCallback = new AsyncCal
lback(WriteEnd);

//創建文件流

myFileStream = new FileStream(@"TT.txt", FileMode.Create);

Console.WriteLine("大量數據寫入! TT.txt");

myFileStream.BeginWrite(b, 0, b.Length, myAsyncCallback, null);

do Console.Write(".");//當數字較大此處才能在在
IsEnd == true
執行輸出多個“.”
while (AsyncHronousIO.IsEnd == false);

myFileStream.Flush();

}

public void WriteEnd(IAsyncResult asyncResult)


...{

AsyncHronousIO.IsEnd = true;

Console.WriteLine("寫入完畢! TT.txt");

}

}