none
Safe delegate invocation technique RRS feed

  • Question

  • Hello.

    When I need to dispatch some data to a form in a multiform app, I widely use that safe code structure

    In some form

    
    
    void ReceiveData(byte[] data, int ID) {
       if (this.InvofeRequired) {
          Invoke(new SomeDelegate(this.ReceiveData, data, ID);
          return; // what should I return here?        
          }
        if (ID == localID) // portion is to me       
         { /*do something */}
        return; 
    }
    
    //and in top code
    
    public static bool Dispatch(byte[], string Id) {
       if (Some == null) return;
       foreach (Delegate d in Some.GetInvocationList()) {
         SomeDelegate r = (SomeDelegate)d;
         r(m, Id)) return;
         }
       return;
    }
    
    // I want to stop that loop if the portion is arrived to its recepient
    



    Now my Question:

    How to do the same if the Delegate has a return Boolean value?

    Thanks





    • Edited by RobbKirk Friday, August 23, 2019 1:46 PM
    Friday, August 23, 2019 1:35 PM

Answers

  • Hi,

    The Invoke method will just return your result, but you need to cast is.

    public bool ReceiveData(byte[] data, int id)
    {
        if (InvokeRequired)
        {
            return (bool)Invoke(new SomeDelegate(ReceiveData), data, id);
        }
    
        if (id == 35) // portion is to me       
        {
            return true;
        }
    
        return false;
    }

    Kind regards,

    Johnny Hooyberghs

    • Marked as answer by RobbKirk Friday, August 23, 2019 2:00 PM
    Friday, August 23, 2019 1:47 PM

All replies

  • Hi,

    The Invoke method will just return your result, but you need to cast is.

    public bool ReceiveData(byte[] data, int id)
    {
        if (InvokeRequired)
        {
            return (bool)Invoke(new SomeDelegate(ReceiveData), data, id);
        }
    
        if (id == 35) // portion is to me       
        {
            return true;
        }
    
        return false;
    }

    Kind regards,

    Johnny Hooyberghs

    • Marked as answer by RobbKirk Friday, August 23, 2019 2:00 PM
    Friday, August 23, 2019 1:47 PM
  • Thank you!
    Friday, August 23, 2019 2:00 PM