SafeInvoke Error

0 votes
asked Apr 21, 2010 by Scott Lezberg (120 points)
edited Jun 19, 2012

I keep getting errors with the SafeInvoke function provided by Rebex - "Invoke or BeginInvoke cannot be called on a control until the window handle has been created."

commented Apr 21, 2010 by Lukas Pokorny (128,250 points)
The SafeInvoke method is a part of several Rebex samples like WinFormClient, WinFormGet, ResumableTransfer (FTP/SFTP), ImapBrowser, Pop3Browser (Mail). Do any of these samples throw the exception you describe or are you using the SafeInvoke method in your application?

1 Answer

0 votes
answered Apr 21, 2010 by Lukas Pokorny (128,250 points)
edited Jun 19, 2012

SafeInvoke method is a simple wrapper around Control.Invoke and just like this .NET method, it requires the Control to actually be fully initialized before it can be used (because it actually uses the Control's message queue to invoke the delegate).

An enhanced version of SafeInvoke should solve this problem:


private void SafeInvoke(Delegate method, params object[] args)
        if (!InvokeRequired)
        else if (!IsDisposed)
            Invoke(method, args);
    catch (ObjectDisposedException)


Private Sub SafeInvoke(ByVal method As [Delegate], ByVal args() As Object)
        If Not InvokeRequired Then
        ElseIf Not IsDisposed Then 
            Invoke(method, args)
        End If
    Catch x As ObjectDisposedException
    End Try
End Sub

The same problem with Control.Invoke method was discussed in microsoft.public.dotnet.framework.windowsforms newsgroup, check it out if you are interested in more information.