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

asked 21 Apr '10, 12:34

Scott%20Lezberg's gravatar image

Scott Lezberg
16
accept rate: 0%

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?

(21 Apr '10, 13:56) Lukas Pokorny ♦♦

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 Teh
            method.DynamicInvoke(args)
        Else
            If 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.

link

answered 21 Apr '10, 14:24

Lukas%20Pokorny's gravatar image

Lukas Pokorny ♦♦
2.2k18
accept rate: 32%

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×22
×1

Asked: 21 Apr '10, 12:34

Seen: 422 times

Last updated: 24 Mar '11, 03:22