Invoke() and BeginInvoke() questions

I have the following codes.
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);
        }

1. If I understand correctly, this.Invoke(del); is like
this.Invoke((MethodInvoker)(delegate()
{
              this.richtextbx.AppendText(str);
}
correct?

2. Then, what does del.Invoke() do?  Why do I need this line of code?
3. I thought that MethodInvoker can be used only for the method that has void type and has no parameter.  But AddGreeting method has a string type parameter.  How come this is working?
4.  I heard that BeginInvoke() is better than Invoke(), so I was going to replace Invoke() wit BeginInvoke(), but I am getting errors on both 1) and 2) when I tried to use BeginInvoke().  Why is that?
private void UpdateUI(MethodInvoker del)
        {
            if(this.InvokeRequired)
                this.BeginInvoke(del);----1)
            else
                del.BeginInvoke();-----2)
        }
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:
Hey IzzyTwinkly,

1. indeed

2. Looks like it is just invoke the given method as a fallback, if there is no need to invoke the given method on the GUI thread

3. It's not actually calling AddGreeting, it's executeing the delegate.

4.
You should probably rethink this approach.  You really only need to use BeginInvoke when you need to have something run asynch. For an action such as setting the text in a textbox, invoke will be fine.


What exception are you getting when you are using BeginInvoke

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
IzzyTwinklyAuthor Commented:
Hi Aruspex,

Thank u so much for your help.
i) so for the second question, I don't need the following part in UpdateUI method, right?
else
      del.Invoke();

ii) U said that MethodInvoker is executing the delegate.  I don't really understand what msdn tells about MethodInvoker: 'MethodInvoker provides a simple delegate that is used to invoke a method with a void parameter list. '  I am using anonymous method here. what does it mean by 'a void parameter list' here?

iii) Lastly if I use BeginInvoke() in 2), I am getting an error saying that no overload method 'BeginInvoke()' takes 0 parameter.  

When a button is clicked, the following codes are executed for the checked items in myArrayList.
.....
for (int i = 0; i < myArrayList.Count; i++)
 {
      // If the checked item is not existing
      if (CheckMyItemExists(myArrayList[i].ToString()) == false)
      {
           MessageBox.Show(myArrayList[i].ToString() + " is not in the List.");
            del = new MethodInvoker(delegate
            {
                    this.ProgressBar1.Value = (i + 1) * 100 / myArrayList.Count;---------------3)
             }
        );
            UpdateUI(del);
            continue;
        }
....//Code for existing checked items
And if I use BeginInvoke() in 1), I am getting "System.DivideByZeroException" on 3)
Could u plz tell me y?
0
AruspexCommented:
Hello,

i) You should leave it in so the delegate will execute if .InvokeRequired() returns false

ii) By no parameters it means a method like Save() instead of Save(true), with true being a parameter.

iii) I would just stick to invoke, as I mentioned before BeginInvoke is async and can be more complex to use correctly.  You would need to create a callback method to pass to del.BeginInvoke() to fix that error

iv)
maybe try something like;



       
if (CheckMyItemExists(myArrayList[i].ToString()) == false)
{
   MessageBox.Show(myArrayList[i].ToString() + " is not in the List.");
    int itemCount = myArrayList.Count;
 
    del = new MethodInvoker(delegate
    {
        if (itemCount != 0)
        {
            this.ProgressBar1.Value = (i + 1) * 100 / itemCount;
        }
        else
        {
            //code to handle 0 item count
        }
     }
 
    UpdateUI(del);
    continue;

Open in new window

0
IzzyTwinklyAuthor Commented:
Thank u very much Aruspex
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.