Question

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.

Was it helpful?

Solution

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();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top