none
线程异步调用,关于过程不懂! RRS feed

  • 问题

  • //(一)主线程中: static void Main(string[] args) { TakesAWhileDelegate d = TakesAWhile; //异步回调. d.BeginInvoke("old", 2500, TakesAWhileCompleted, d); //for(int i = 0; i < 100; i++) { // Console.WriteLine("continue..."); // Thread.Sleep(10); //} Console.ReadKey(); } //(二): //(线程)委托定义. private delegate string TakesAWhileDelegate(string data, int time); //委托线程执行的方法. private static string TakesAWhile(string data, int time) { Console.WriteLine("TakesAWhile started."); Thread.Sleep(time); Console.WriteLine("TakesAWhile completed."); return null; } //委托线程完成后调用. static void TakesAWhileCompleted(IAsyncResult ar) { if(ar == null) throw new ArgumentNullException("ar"); TakesAWhileDelegate d1 = ar.AsyncState as TakesAWhileDelegate; Trace.Assert(d1 != null, "Invalid object type"); string result = d1.EndInvoke(ar); Console.WriteLine("result: {0}", result); }

    1,"//委托线程完成后调用."方法中的参数"ar"什么时候为空哈?文档说:
    The result of the asynchronous operation(ar).
    因为我以为"ar"是接受"//委托线程执行的方法."传过来的.但是我作为"null"时,异常"if(ar == null) throw new ArgumentNullException("ar");"并没有被抛出.
    


    2013年6月16日 3:28

答案

  • namespace Csharp
    {
        public class Program
        {
            private delegate string TakesAWhileDelegate(string dataint time);
            //委托线程执行的方法.
            private static string TakesAWhile(string dataint time)
            {
                Console.WriteLine("TakesAWhile started.");
                Thread.Sleep(time);
                Console.WriteLine("TakesAWhile completed.");
                return "Finished";
            }
            //委托线程完成后调用.
            static void TakesAWhileCompleted(IAsyncResult ar)
            {
                
                if (ar.AsyncState == nullthrow new ArgumentNullException("ar");
     
                TakesAWhileDelegate d1 = ar.AsyncState as TakesAWhileDelegate;
                Trace.Assert(d1 != null"Invalid object type");
     
                string result = d1.EndInvoke(ar);
                Console.WriteLine("result: {0}"result);
            }
     
            static void Main(string[] args)
            {
                TakesAWhileDelegate d = TakesAWhile;
     
                //异步回调.
                d.BeginInvoke("old"2500TakesAWhileCompletedd);
                Console.ReadKey();
            }
        }
    }

    If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer

    Help by clicking:
    Click here to donate your rice to the poor
    Click to Donate
    Click to feed Dogs & Cats


    Found any spamming-senders? Please report at: Spam Report

    2013年6月16日 4:25
    版主
  • 如果有返回值的话,第四个参数一般不可以省略——因为我们需要通过这个参数获取原本的委托对象,并调用EndInvoke。

    如果没有返回对象,那么这个参数的确无所谓了。


    If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer

    Help by clicking:
    Click here to donate your rice to the poor
    Click to Donate
    Click to feed Dogs & Cats


    Found any spamming-senders? Please report at: Spam Report

    2013年6月17日 1:29
    版主

全部回复

  • 你的“d”是TakesAWhile,这个一定不是为空。

    除非你认为对BeginInvoke的最后一个参数赋值为空。


    If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer

    Help by clicking:
    Click here to donate your rice to the poor
    Click to Donate
    Click to feed Dogs & Cats


    Found any spamming-senders? Please report at: Spam Report

    2013年6月16日 3:35
    版主
  • //(一)刚我看错了,"IAsyncResult"有三个值:  object AsyncState, WaitHandle AsyncWaitHandle,bool CompletedSynchronously,bool IsCompleted.所以 IAsyncResult不是直接接收"TakesAWhile(string data, int time) {  //委托线程执行的方法."的返回值.
    //(二)也不行哈.我在最后的方法置为空,也没有被抛出...虽然说 "TakesAWhileCompleted(IAsyncResult ar) { //委托线程完成后调用."方法可以访问 BeginInvoke()方法的最后一个参数...
    

    2013年6月16日 3:53
  • //
    //主线程中:
    d.BeginInvoke(1, 3000, TakeAWhileCompleted, null);
    时,
    //TakeAWhileCompleted(IAsyncResult ar) 抛出的是这个:
    Trace.Assert(d != null, "Invalid Object type!");..

    2013年6月16日 3:57
  • 对不起,没有看清楚。

    BeginInvoke中其中最后一个参数object实质传入了IAsyncCallBack中,这样才会引发异常:

    //委托线程完成后调用.
            static void TakesAWhileCompleted(IAsyncResult ar)
            {
                if (ar.AsyncState == nullthrow new ArgumentNullException("ar");
            }


    If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer

    Help by clicking:
    Click here to donate your rice to the poor
    Click to Donate
    Click to feed Dogs & Cats


    Found any spamming-senders? Please report at: Spam Report

    2013年6月16日 4:21
    版主
  • namespace Csharp
    {
        public class Program
        {
            private delegate string TakesAWhileDelegate(string dataint time);
            //委托线程执行的方法.
            private static string TakesAWhile(string dataint time)
            {
                Console.WriteLine("TakesAWhile started.");
                Thread.Sleep(time);
                Console.WriteLine("TakesAWhile completed.");
                return "Finished";
            }
            //委托线程完成后调用.
            static void TakesAWhileCompleted(IAsyncResult ar)
            {
                
                if (ar.AsyncState == nullthrow new ArgumentNullException("ar");
     
                TakesAWhileDelegate d1 = ar.AsyncState as TakesAWhileDelegate;
                Trace.Assert(d1 != null"Invalid object type");
     
                string result = d1.EndInvoke(ar);
                Console.WriteLine("result: {0}"result);
            }
     
            static void Main(string[] args)
            {
                TakesAWhileDelegate d = TakesAWhile;
     
                //异步回调.
                d.BeginInvoke("old"2500TakesAWhileCompletedd);
                Console.ReadKey();
            }
        }
    }

    If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer

    Help by clicking:
    Click here to donate your rice to the poor
    Click to Donate
    Click to feed Dogs & Cats


    Found any spamming-senders? Please report at: Spam Report

    2013年6月16日 4:25
    版主
  • //其实和"第四个"参数没有直接关联的...
    d.BeginInvoke(1, 3000, ar => {
                    int result = d.EndInvoke(ar);
                    Console.WriteLine(result);
                }, null);
    
    用lambda委托....可以...

    2013年6月16日 9:40
  • 有关系,看我第二次回复的帖子代码。

    If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer

    Help by clicking:
    Click here to donate your rice to the poor
    Click to Donate
    Click to feed Dogs & Cats


    Found any spamming-senders? Please report at: Spam Report

    2013年6月16日 9:42
    版主
  • 我看了哈...我的意思是...如果在第三个参数..用 lambda直接访问结果的话...就不用管第四个参数了...貌似第四个参数是随意的对象都行...我知道在"//委托线程完成后调用. static void TakesAWhileCompleted(IAsyncResult ar) { "

    方法中可以通过获取"委托d回调"...

    2013年6月16日 16:18
  • 如果有返回值的话,第四个参数一般不可以省略——因为我们需要通过这个参数获取原本的委托对象,并调用EndInvoke。

    如果没有返回对象,那么这个参数的确无所谓了。


    If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer

    Help by clicking:
    Click here to donate your rice to the poor
    Click to Donate
    Click to feed Dogs & Cats


    Found any spamming-senders? Please report at: Spam Report

    2013年6月17日 1:29
    版主
  • d.BeginInvoke(1, 3000, ar => { int result = d.EndInvoke(ar); Console.WriteLine(result); //这个就有返回值了吧...只是我们在第三步就直接通过lambda调用EndInvoke方法而已..

    不是么...

    2013年6月18日 2:09
  • 这样做当然可以,因为你的d是外边已经定义的变量。

    If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer

    Help by clicking:
    Click here to donate your rice to the poor
    Click to Donate
    Click to feed Dogs & Cats


    Found any spamming-senders? Please report at: Spam Report

    2013年6月18日 2:13
    版主