Link to home
Start Free TrialLog in
Avatar of IzzyTwinkly
IzzyTwinklyFlag for United States of America

asked on

MethodInvoker

Hi

To avoid cross thread issue, I origianlly used the following codes
Form A:
public delegate void GreetingDelegate(string str);
        public void AddGreeting(string str)
        {
            if (this.richTextBox1.InvokeRequired)
            {
                this.richTextBox1.Invoke(new GreetingDelegate(AddGreeting), str);
                return;
            }
            else
            {
                this.richTextBox1.AppendText(str);
            }
        }    
Then I changed the code as follow:
Form B:
private void UpdateUI(MethodInvoker del)
        {
            if(this.InvokeRequired)
                this.Invoke(del);
            else
                del.Invoke();
        }
public void AddGreeting(string str)
        {
            MethodInvoker del = new MethodInvoker(delegate
            {
                this.richtextbx.AppendText(str);
            }
            );
            UpdateUI(del);
        }
When UpdateUI(del) is called, I guess that UpdateUI(del) method does following
Form C:
if(this.InvokeRequired)
{
    this.Invoke((MethodInvoker)(delegate()
                                                {
                                                   this.richtextbx.AppendText(str);
                                                  }
}
else
{
       this.richtextbx.AppendText(str);
 }
What I mean is when UpdateUI(del) is called, I know that it becomes like Form C 'by experience',and I think I understand, why this.Invoke(del) becomes
this.Invoke((MethodInvoker)(delegate()
                                                {
                                                   this.richtextbx.AppendText(str);
                                                  }
Please correct me if I am wrong.
I think the reason is that 'del' is pointing to the "delegate" that executes the anonymouse method and 'this.richtextbx.AppendText(str);' is what anonymous method contains in its body, so it makes sense that it Invoke(del) is like "Hey, I am invoking the delegate you are passing(in this case, it's del)".
 However, I don't understand why del.Invoke() becomes 'this.richtextbx.AppendText(str);'  Can Invoke() method have 0 parameter like this?

Also MethodInvoker is itself a delegate, right?  this delegate execute AddGreeting method.  But msdn says, "MethodInvoker provides a simple delegate that is used to invoke a method with a void parameter list." but AddGreeting has a parameter str.  How can MethodInvoker be used to invoke a method with a parameterlist here?

Avatar of Aruspex
Aruspex
Flag of Australia image

Hello IzzyTwinkly,

You are right on track and I must say, very good question.

Basically the delegate has access to the local scope of the method that it was defined in.
That is why it knows what the value of str is. So str is not actually a parameter of del.
Rather it knows the value of str because del is defined within AddGreeting

Hope that helps.
Avatar of IzzyTwinkly

ASKER

Thanks again Aruspex, but I need more detailed answers.  I am still a beginner.
ASKER CERTIFIED SOLUTION
Avatar of Aruspex
Aruspex
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks you guys~