how to use Invoke method in a file of extensions/methods?
Asked Answered
C

5

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.

Carlos answered 16/12, 2011 at 16:39 Comment(2)
You can use the existing Action delegate; you don't need to create your own.Caponize
Maybe, (label.TopLevelControl as Form).InvokeGanges
A
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.

Adamant answered 16/12, 2011 at 16:46 Comment(0)
P
3

Change

Invoke((UpdateState)delegate …

to

label.Invoke((UpdateState)delegate …
Peptidase answered 16/12, 2011 at 16:42 Comment(1)
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.Tetragonal
H
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)

Highspirited answered 16/12, 2011 at 16:42 Comment(0)
C
1

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

Caponize answered 16/12, 2011 at 16:43 Comment(0)
S
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.

Sharpe answered 16/12, 2011 at 16:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.