Cannot call Invoke or InvokeAsync on a control until the window handle has been created

R

rawCoder

Hi

I am having this InvalidOperationException with message Cannot call Invoke
or InvokeAsync on a control until the window handle has been created

This is raised when i try to invoke a method with arguments
I need to do this as the method is being called Asynchronously from
elsewhere using delegate's BeginInvoke method.
The form is well created/initialized yet the messages states otherwise.

Cant extract a sample code out of my app.
Let me know if the code is needed for the problem.

All controls on the form are in a panel. The form itself isnt show, but the
panel is shown as a child control of some other panel. Can this be the
problem.

Thank you
rawCoder
 
N

Nicholas Paldino [.NET/C# MVP]

rawCoder,

I would be more inclined to believe the framework. Basically, the
handle to the control that you are calling Invoke on doesn't exist at the
time of the call. Are you doing this in the constructor, perhaps, or
somewhere else? Has the control been removed and not yet disposed?

You might be better off calling Invoke on the main form, instead of one
of the child window controls (in the end, it doesn't really matter which
control you call it on, as it will most likely all be on the same thread).

If you could post some code, it would help.
 
R

rawCoder

Well actually i am calling it for a form ... the code i want to run is on
form level... heres some code, hope that might help.


Lets say this is a method in event management class and is called when some
intimator wants to raise a event and pass the data to the receiver
Public Sub DataReceivedASynchRaiser(ByVal Data As String, Optional ByVal
IsInbound As Boolean = True)

Dim del As DataReceivedDelegate

' TODO: TRICK WARNING - undocumented way to raise event by appending
event with word Event

For Each del In m_DataReceivedEvent.GetInvocationList()

del.BeginInvoke(Data, Nothing, Nothing)

Next

End Sub

The delegate being called is like
Public Delegate Sub DataReceivedDelegate(ByVal Data As String )

and the event is declared as

Public Event m_DataReceived As DataReceivedDelegate



now lets say that the form that wants to receive the intimation has

'Delegate Sub OnDataReceivedDelegate(ByVal data As String) '* Doesnt get
called

Delegate Sub OnDataReceivedDelegate(ByVal args() As Object)

Public Sub OnDataReceived(ByVal data As String)

Dim x As New OnDataReceivedDelegate(AddressOf OnDataReceivedHandler)

Dim args() As Object = {data}

Me.Invoke(x, args) ' InvalidOpException

'Me.Invoke(x, data) '* Doesnt get called

End Sub

'Public Sub OnDataReceivedHandler(ByVal data As String) '* Doesnt get called

Public Sub OnDataReceivedHandler(ByVal args() As Object)

End Sub

Now the thing to note is that if i use the delegate that has data as
argument, nothing happens ( the control doesnt switch) on the invoke call
but when i use the args argument then that exception occurs.

The first code is from a singleton class used to raise and receive (call)
event and pass data. the second is the form which wants to receive the data.

Hope now the problem is clear. i am not sure if the code would work on empty
project as i have copied it directly from my project.

Lemme know wat mistake i am doing.

Thanx

rawCoder







Nicholas Paldino said:
rawCoder,

I would be more inclined to believe the framework. Basically, the
handle to the control that you are calling Invoke on doesn't exist at the
time of the call. Are you doing this in the constructor, perhaps, or
somewhere else? Has the control been removed and not yet disposed?

You might be better off calling Invoke on the main form, instead of one
of the child window controls (in the end, it doesn't really matter which
control you call it on, as it will most likely all be on the same thread).

If you could post some code, it would help.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

rawCoder said:
Hi

I am having this InvalidOperationException with message Cannot call Invoke
or InvokeAsync on a control until the window handle has been created

This is raised when i try to invoke a method with arguments
I need to do this as the method is being called Asynchronously from
elsewhere using delegate's BeginInvoke method.
The form is well created/initialized yet the messages states otherwise.

Cant extract a sample code out of my app.
Let me know if the code is needed for the problem.

All controls on the form are in a panel. The form itself isnt show, but
the
panel is shown as a child control of some other panel. Can this be the
problem.

Thank you
rawCoder
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top