7

Well, I'm writing a file of extensions/method useful to strings,label,linklabels,class etc.

but, I have a problem. I have an showMessage() method that change the Text of label, works fine. But I decide to do that works with thread execution, then I do this:

namespace LabelExtensions
{
    public static class LabelExtensionsClass
    {        
        private delegate void UpdateState();

        public static void ShowMessage(this Label label, string text)
        {
            if (label.InvokeRequired)
            {
                label.Invoke((UpdateState)delegate
                {
                    label.Text = text;
                });
            }
            else
            {
                  label.Text = text;
            }
        }
}
}

sorry, it was a typo. I typed this code on forum. the error continue.

according the documentation,to use the Invoke method need to imports:

Namespace: System.Windows.Forms

Assembly: System.Windows.Forms (in System.Windows.Forms.dll)

then I did:

using System.Windows.Forms;

but this returns same error:

The name 'Invoke' does not exist in the current context

how I fix this?

Thanks in advance.

The Mask
  • 17,007
  • 37
  • 111
  • 185

5 Answers5

8

Why not just do this:

label.BeginInvoke( (Action) (() => label.Text = text));

There is no need to create your own delegate. Just use the built-in Action delegate. You should probably create your extension method for the base Control class instead of the Label class. It'll be more reusable.

Jason Down
  • 21,731
  • 12
  • 83
  • 117
3

Change

Invoke((UpdateState)delegate …

to

label.Invoke((UpdateState)delegate …
Ondrej Tucny
  • 27,626
  • 6
  • 70
  • 90
  • Exactly. The "Invoke" method you need is a member of the System.Windows.Forms.Control class, from which Label is derived. Your static LabelExtensions class doesn't have an "Invoke" method. – KeithS Dec 16 '11 at 16:44
1

You forgot to specify the label in your code (when you call the Invoke method):

public static void ShowMessage(this Label label, string text)
        {
            if (label.InvokeRequired)
            {
                lablel.Invoke((UpdateState)delegate
                {
                    label.Text = text;
                });
            }
            else
            {
                  label.Text = text;
            }
        }

also, consider using BeginInvoke instead so you won't block the calling thread (if applicable)

Asher
  • 1,867
  • 15
  • 24
1

Invoke is an instance method of Control.
You need a Control to call it on, such as your label.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
1

You don't need to declare a new delegate type, or construct a new lambda or anonymous delegate. You already have a method that acts on the UI thread - the one you are writing. Just make it call itself on the UI thread if needed, like this.

public static void ShowMessage(this Label label, string text) {

    if(label.InvokeRequired) {
        label.Invoke(new Action<Label, string>(ShowMessage), label, text);
        return;
    }

    label.Text = text;
}

The advantage of this approach is that you can almost copy and paste the redirection code block from this method to any other method that you want to modify in the same way.

Christian Hayter
  • 30,581
  • 6
  • 72
  • 99