异步写文件非线程

//非线程异步写文件
        public void WriteFile()
        {
            string str = "bbbbbbbb0测试"+"\r\n";
            FileStream fs = new FileStream(@"C:\Test.txt", FileMode.Append, FileAccess.Write, FileShare.None);
            byte[] buffer = System.Text.Encoding.Unicode.GetBytes(str);
            //byte[] buffer = new byte[100];           
            //for (int i = 0; i < buffer.Length; i++)
            //{
            //    buffer[i] =(byte)2;
            //}
            fs.BeginWrite(buffer, 0, buffer.Length, FileCallBack, fs);
        }

        static void FileCallBack(IAsyncResult asyncResult)
        {
            FileStream fs = asyncResult.AsyncState as FileStream;
            if (fs != null)
            {
                fs.EndWrite(asyncResult);
                fs.Close();
            }
        }

 

 

线程

//多线程操作同步写文件方法
public
ThreadOperate()
{
    Thread t = new Thread(new ThreadStart(ThreadProc));
    t.Start();
}

//同步写文件方法
public static void ThreadProc()
{

posted on 2009-07-14 09:27  青春的虎子  阅读(262)  评论(0)    收藏  举报

导航