Answered why use delegate in Arbiter?

  • samedi 10 décembre 2011 06:19
     
     

    Hi everybody

    I have a question. Why use delegate in receiver?

    Please help me about this.

     

Toutes les réponses

  • dimanche 11 décembre 2011 22:09
    Propriétaire
     
     

    This is a fundamental programming question. Do you know C#?

    Trevor

     

  • lundi 12 décembre 2011 06:16
     
     

    I know C# slightly and I try to learn about that more.

    Please help me about using it in Arbiter or refer me to where I'm about to give information.

    Thanks for your help.

  • lundi 12 décembre 2011 07:33
    Propriétaire
     
     

    I'm not really clear on what your question is. When you set up a receiver you have to specify the method that will handle the message.

    On the other hand, a lot of code that uses yield return uses lambdas. These can use the delegate keyword to write an in-line method to handle the response to a request. However, you don't have to use delegate, you can write lambdas in a much more compact syntax.

    Maybe you can post a specific code snippet that you do not understand.

    Trevor

     

     

  • lundi 12 décembre 2011 16:24
     
      A du code
    yield return Arbiter.Receive(false, p1, delegate(int i)
                    {
                        Console.WriteLine("P1 Thread {0}: {1}",  Thread.CurrentThread.ManagedThreadId, i);
                        p2.Post(i + 1);
                    });
    


    For example I don't know delegate what does in this code when we post a message to p1 what happen?

    I read delegate's help of Microsoft visual studio 2010 and I understand that delegate is a pointer to function and use for callback.

    I think we use delegate for a function and call this with delegate but I don't understand that in upper code we define delegate in arbiter.receiver or not?

  • mercredi 14 décembre 2011 07:59
    Propriétaire
     
     Traitée

    The delegate in this case is an in-line unnamed function, also referred to as a lambda although most people use a newer syntax for lambdas that does not use the delegate keyword. As I said previously, this is really a C# programming question, not so much RDS.

    The effect of this code is that a receiver is created, it is activated (by the yield return), and then when p1 receives a message the code inside the delegate is executed. The code execution of the enclosing code pauses at this point (because of the yield return) but does not block the thread. Instead the thread is returned to the queue for other tasks. A thread is then allocated, maybe the same one, or maybe a different one, when the message arrives so that the delegate can be executed. In this sense the delegate is a task that waits for a message. When the delegate executes, the enclosing code resumes execution.

    If you did not use yield return but explicitly Activate the receiver, then the code will continue executing and the method that it is in could even return to the caller. Some time later the delegate will be executed because it still hangs around.

    Trevor

     

     

    • Marqué comme réponse Samim_Tele samedi 17 décembre 2011 10:34
    •