0 votes
by (120 points)
edited

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."

by (144k 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
by (144k points)
edited

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:

C#:

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

VB.NET:

Private Sub SafeInvoke(ByVal method As [Delegate], ByVal args() As Object)
    Try
        If Not InvokeRequired Then
            method.DynamicInvoke(args)
        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.

...