0

My main Form, named Form1 has a class with an event which indicates that Form1 should be Closed.

When I receive the event and try to Close I get the excepcion that a control.invoke must be used. Fine, I make the call with this code and I'm still getting the same exception:

void Prox_OkEvent(object sender, EventArgs e)
{
    if (this.InvokeRequired)
    {
        callControlhandler c = new callControlhandler(CloseForm);
        c.Invoke();
    }
    else Close();
}

private void CloseForm()
{
    try { this.Close(); }
    catch (Exception e) { MessageBox.Show(e.Message); }
}

Where is the problem? thanks for any help

Ignacio Gómez
  • 1,587
  • 4
  • 23
  • 41

1 Answers1

4

Invoke should be called against the Form itself. In this case it is called against c.

Try...

        if (this.InvokeRequired)
        {
            this.Invoke(new Action(CerrarForm));
        }
P.Brian.Mackey
  • 43,228
  • 68
  • 238
  • 348
  • 2
    +1 The point of `Invoke` is to pass it to the GUI thread. OP's code is simply invoking the delegate on the current thread. – vgru Feb 11 '13 at 14:40