MyException - 我的异常网
当前位置:我的异常网» C# » 异步调用System.IO.FileStream的BeginRead的返回值I

异步调用System.IO.FileStream的BeginRead的返回值IAsyncResult的IsCompleted属性的有关问题(2)

www.MyException.Cn  网友分享于:2013-02-21  浏览:105次

                Console.Write(".");
            }

            // Call EndInvoke to retrieve the results.
            string returnValue = caller.EndInvoke(out threadId, result);

            Console.WriteLine("\nThe call executed on thread {0}, with return value \"{1}\".",
                threadId, returnValue);
            Console.Read();

        }
    }
}


//IsCompleted判断有问题的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Threading;

namespace chapter4_thread_5
{
    class Program
    {
        static void Main(string[] args)
        {
            APM_4_1();
        }

        /// <summary>
        /// 采用主线程轮询判断异步处理是否完成的方式解决异步编程的同步问题
        /// </summary>
        private static void APM_4_1()
        {
            Console.WriteLine("主程序中!");
            Console.WriteLine("线程的 Id: {0}", Thread.CurrentThread.ManagedThreadId);
            Console.WriteLine();

            string path = "../../demo.log";
            byte[] buffer = new byte[4096];

            System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.Open);
            IAsyncResult result = fs.BeginRead(buffer, 0, 4096,
                delegate(IAsyncResult ar)
                {
                    Thread.Sleep(6000);
                    Console.WriteLine("第三步操作中。");
                    Console.WriteLine("线程的 Id: {0}", Thread.CurrentThread.ManagedThreadId);
                    int length = fs.EndRead(ar);
                    string message = System.Text.Encoding.UTF8.GetString(buffer, 0, length);

文章评论

软件开发程序错误异常ExceptionCopyright © 2009-2015 MyException 版权所有