class Program
    {
        public delegate void takedelegate(int ms);
        public static void takeMethod(int ms)
        {
            Thread.Sleep(ms);
        }
        static void Main(string[] args)
        {
            takedelegate dl = takeMethod;
            IAsyncResult ar = dl.BeginInvoke(3000, null, null);
            Console.WriteLine("开始执行");
            for (int i = 0; i < 100; i++)
            {
                Console.Write(".");
                Thread.Sleep(50);
                if (ar.IsCompleted)  //当异步调用执行完后,退出循环
                {
                    Console.WriteLine("执行完毕 ");
                    break;
                }
            }
            Console.Read();
         }
     }  我想问ar.IsCompleted属性,判断异步调用是否完成,BeginInvoke方法开始执行,就立即返回,此时ar.IsCompleted属性值为false,当异步调用执行完后,ar.IsCompleted属性值为true,不过,这个true值,是怎么传进for循环的啊?

解决方案 »

  1.   

    不是传到for 循环内。你需要先了解引用类型
      

  2.   

    EndInvoke的原型是变化的,原因之前已经说过了。当你的方法参数有out 参数时,out 参数的返回值就需要从 EndInvoke 中获取返回相应的值就是EndInvoke的职责之一
      

  3.   

    引用类型,我知道啊,就是两个引用类型的变量如果相等,其中一个变量的值会随着另一个的值的改变而改变
    不过,这里的话,BeginInvoke方法从开始执行,就返回一个IAsyncResult接口,不是只有一个变量吗?
    怎么理解"其中一个变量的值会随着另一个的值的改变而改变"呢?
      

  4.   

    IsCompleted 属性是跟着 ar 变量走的,BeginInvoke 会在内部对这个 ar 所指向的对象的属性进行修改,然后 ar 也会立即反应出来,因为他们是指向的同一个对象。这跟 for 循环没有关系。
      

  5.   

    使用Backgroundworker BackGroundworker worker=new BackGroundworker();  
      worker.DoWork+=new DoWorkEventHandler((sender,e)=>  
      {  
      //ToDo 
      });    worker.ProgressChanged+= new ProgressChangedEventHandler((sender,e)=>  
      {
      //ToDo 
      });    worker.RunWorkerCompleted+=new RunWorkerCompletedEventHandler((sender,e)=>  
      {
      //ToDo
      });然后在根据你得业务需要去做吧!
      

  6.   


    public class CustomClass
            {
                public bool IsCompleted { set; get; }
            }        public class MockDelegate
            {
                CustomClass cc;
                
                public CustomClass Begin()
                {
                    new Thread(_ => { Thread.Sleep(3000); End(); }).Start();
                    return cc = new CustomClass();
                }
                public void End()
                {
                    cc.IsCompleted = true;
                }
            }        static void Main(string[] args)
            {            MockDelegate md = new MockDelegate();                        CustomClass ar = md.Begin();
                Console.WriteLine("开始执行");
                for (int i = 0; i < 100; i++)
                {
                    Console.Write(".");
                    Thread.Sleep(50);
                    if (ar.IsCompleted)
                    {
                        Console.WriteLine("执行完毕 ");
                        break;
                    }
                }
                Console.Read();
         }
      

  7.   


            public class CustomClass
            {
                public bool IsCompleted { set; get; }
            }        public class MockDelegate
            {
                CustomClass cc;
                
                public CustomClass Begin(Action dosth)
                {
                    new Thread(_ => { dosth(); End(); }).Start();
                    return cc = new CustomClass();
                }
                public void End()
                {
                    cc.IsCompleted = true;
                }
            }        static void dosth(int ms)
            {
                Thread.Sleep(ms);
            }        static void Main(string[] args)
            {            MockDelegate md = new MockDelegate();            CustomClass ar = md.Begin(() => dosth(3000));
                Console.WriteLine("开始执行");
                while (true)
                {
                    Console.Write(".");
                    Thread.Sleep(50);
                    if (ar.IsCompleted)
                    {
                        Console.WriteLine("执行完毕 ");
                        break;
                    }
                }
                Console.Read();
            }