Forms/Threading problem

O

Oenone

Wonder if someone can help me out here as I'm not making any progress with
this...

I have an application which uses an MDI window. When it is running, the
application creates an instance of a COM object, passes into it an instance
of a local .NET class from my application, and then calls a method of the
COM object.

The COM object uses a method (ShowInfoForm) in the provided .NET object to
display an information form within the MDI container. The ShowInfoForm
function itself calls a method on my MDI form (GetInfoForm) which actually
creates and displays the information form.

\\\ In my MDI form
'The GetInfoForm function opens and returns a new
'instance of the InfoForm
Public Function GetInfoForm() As InfoForm
Dim f As New InfoForm
f.MdiParent = Me
f.Show()
Return f
End Function
///

However, when the .NET class method is called from within the COM object, it
is running in a different thread to that of my main application (for reasons
I don't understand). This therefore leads to an error when my
MDI.GetInfoForm method tries to set the .MdiParent property of the
information form:

A first chance exception of type 'System.ArgumentException' occurred in
system.windows.forms.dll

Additional information: Controls created on one thread cannot be
parented
to a control on a different thread.

When I've had threading issues with forms before, I've always used the
Invoke method on the form to execute the code in the form's own thread. I
tried to do that here by using the following code:

\\\ In my .NET class:
'Create a delegate for the InfoForm function.
Public Delegate Function GetInfoForm_Delegate() As InfoForm

Public Function ShowInfoForm

Dim inf As InfoForm
Dim getInfoDelegate As GetInfoForm_Delegate

'Point the delegate to the GetInfoForm function in my
'MDI form object instance
getInfoDelegate = New GetInfoForm_Delegate(AddressOf
AppMDI.GetInfoForm)
'*** Problem occurs on the next line ***
inf = DirectCast(AppMDI.Invoke(getInfoDelegate), InfoForm)

End Function
///

When the code gets to the point of executing the AppMDI.Invoke method
(indicated above), this call never returns. My application sits there
completely locked until I eventually click the Stop button in the IDE. If I
put a breakpoint in the MDI.GetInfoForm function, the breakpoint does not
get hit.

Does anyone have any suggestions or clues as to how I can get this to work?
I just cannot see what I'm doing wrong here.

My thanks in advance,
 
O

Oenone

Oenone said:
'*** Problem occurs on the next line ***
inf = DirectCast(AppMDI.Invoke(getInfoDelegate), InfoForm)

I should add that if I break into the code when it locks up on this line,
the main thread (that created the AppMDI object) is still on the line of
code that calls into my COM object.

Is it possible that the .Invoke method is trying to get that thread to do
some work, but the thread is already occupied by calling the COM object and
so is unable to respond?
 

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