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

asked on

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)
        }
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
Avatar of IzzyTwinkly

ASKER

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?
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

Thank u very much Aruspex