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?

IzzyTwinklyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AruspexCommented:
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.
0
IzzyTwinklyAuthor Commented:
Thanks again Aruspex, but I need more detailed answers.  I am still a beginner.
0
AruspexCommented:
No worries,

So, what we are doing here is like this.

the delgate del is a wrapper for the statement
 this.richtextbx.AppendText(str);

Maybe look at it like this
public void del()
{
this.richtextbx.AppendText(str);
}

So when this is executed by using invoke() you could look at it like the above.
this is why methodinvoker will work  even though AddGreeting has the parameter str.
ie it is executing the statement  this.richtextbx.AppendText(str), not the whole AddGreeting(str) method. Just the statement and not the method AddGreeting(str)

Now you may ask, but how does del know what the value of str is without passing str in as a parameter.
The answer is tricky!
When the statement this.richtextbx.AppendText(str); is executed by .Invoke, the runtime is effectively executing the statement as if it has been directly called from within AddGreeting(). So it knows what the value of str is.


Hope this helps
 




















0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
saraganiCommented:
Let's try to understand anonymous types and methods

With anonymous types you do something like this:

var ObjectName = new {
    Property1 = "Something",
    property2= 1234,
    Property3 = 123.456
};

With anonymous methods you do something like this (for example):

delegate del = delegate(int integer)
    {
        return (integer %2 == 0);
    }

In this case the delegate gets an integer as an argument.

The beauty of anonymous methods and types is that it lets you define types and methods in run time other than design time.

In case you wondered what Delegate is, then it is a pointer to a function.

When you did this:
delegate del = delegate(int integer)
    {
        return (integer %2 == 0);
    }

You defined a function in runtime like this:
private bool myfunction(int Integer)
{
    return (integer %2 == 0);
}

and then you had del pointing at it.


MethodInvoker has a signature of: void myFunction()
(Void and no arguments).

So when you did this:
 this.Invoke((MethodInvoker)(delegate()
 {
     this.richtextbx.AppendText(str);
 }

The delegate was void and the () tells that it doesn't have any arguments. This is why you can cast it to MethodInvoker.

The str itself is not an argument. It is just a variable which is defined in the scope that both str and the delegate exists in.

this.richtextbx.AppendText gets the pointer to the string
0
IzzyTwinklyAuthor Commented:
Thanks you guys~
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.