IzzyTwinkly
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.InvokeR equired)
{
this.richTextBox1.Invoke(n ew GreetingDelegate(AddGreeti ng), str);
return;
}
else
{
this.richTextBox1.AppendTe xt(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.AppendTex t(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.AppendTex t(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?
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.InvokeR
{
this.richTextBox1.Invoke(n
return;
}
else
{
this.richTextBox1.AppendTe
}
}
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
}
);
UpdateUI(del);
}
When UpdateUI(del) is called, I guess that UpdateUI(del) method does following
Form C:
if(this.InvokeRequired)
{
this.Invoke((MethodInvoker
{
this.richtextbx.AppendText
}
}
else
{
this.richtextbx.AppendText
}
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
{
this.richtextbx.AppendText
}
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.AppendTex
However, I don't understand why del.Invoke() becomes 'this.richtextbx.AppendTex
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?
ASKER
Thanks again Aruspex, but I need more detailed answers. I am still a beginner.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks you guys~
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.