views:

130

answers:

1

Hello,

I'd like to get the handle of my form from a different class(probably thread). I want to do it the way I do invoke

    public int GetHandle
    {
        get
        {
            if (this.InvokeRequired)
            {
                this.Invoke((MethodInvoker)delegate
                {
                    return this.Handle.ToInt32();
                });
            }
        }
    }

I get an error "Since 'System.Windows.Forms.MethodInvoker' returns void, a return keyword must not be followed by an object expression"

If i don't use the invoke,I get an exception that I'm not calling the method from the current thread.

+1  A: 

You can invoke any delegate, not just MethodInvoker. Try this:

public int GetHandle
    {
        get
        {
            if (this.InvokeRequired)
            {
                return (int)this.Invoke((GetHandleDelegate)delegate
                {
                    return this.Handle.ToInt32();
                });
            }
            return this.Handle.ToInt32();
        }
    }
private delegate int GetHandleDelegate();
Nick Whaley
Thanks!Comment.Length = 10;
John