0

I'm using this small utility function:

public static void Invoke(Control control, Action method)
{
    if (control.InvokeRequired)
    {
        if (control.IsDisposed || !control.IsHandleCreated || !control.Created)
            return;
        control.Invoke(method);
    }
    else
        method();
}

Despite all of those sanity checks, when I close my application, a stray invoke always produces this error:

Invoke or BeginInvoke cannot be called on a control until the window handle has been created.

This, despite there clearly being a check to see if the handle is created... What else can I do?

Reinderien
  • 11,755
  • 5
  • 49
  • 77

1 Answers1

2

Read through this thread which delves into Invoke'ing. The problem you're experiencing is almost certainly down to a control disappearing between the if(...) return; and the Invoke doing its thing.

Community
  • 1
  • 1
Will A
  • 24,780
  • 5
  • 50
  • 61
  • 1
    That was indeed helpful. When I replaced all usage of Invoke with BackgroundWorker (whose progress report event had to be somewhat abused to achieve the desired behaviour) it runs without complaint. – Reinderien May 24 '11 at 06:16