IPC-Remoting: Threading issue in MDI-application

M

Michael Maes

Hello,

I have an Mdi-application which also is a remoting-server (Ipc).
Several instances can co-exist on the same machine.

I have a small app (search-engine) (remoting-client) which calls a sub on
the MDI (specific instance) thus retrieving eg the data of a certain customer.
If the customer childform is already instanciated, there is no problem.
If the destination-childform is not yet open, it will be opened like this:

' Create an instance of the form
If Childform.frmInstallations Is Nothing Then
Childform.frmInstallations = New frmInstallations
With Childform.frmInstallations
.MdiParent = MDI ' This variable creates a threading-issue
.Show(Commands.ReadAllowed("InstallationsOverview"))
End With
Else

It is the assignment of the MdiParent which poses the problem.
'MDI' is a variable which references the instance of the Main (mdi) Form
(frmMain).
The generated exception is this: "Cross-thread operation not valid: Control
'frmMain' accessed from a thread other than the thread it was created on."

How can I solve this threading-issue?

TIA,


Michael
 
G

Guest

It is the assignment of the MdiParent which poses the problem.
'MDI' is a variable which references the instance of the Main (mdi)
Form (frmMain).
The generated exception is this: "Cross-thread operation not valid:
Control 'frmMain' accessed from a thread other than the thread it was
created on."

How can I solve this threading-issue?

Just Google the exceptionm you need to marshal the form to the parent
through with Control.BeginInvoke (don't use Invoke as it can deadlock).
 
M

Michael Maes

Hi,

I came up with this solution:

#Region " Declarations "

Module Declarations

Friend fMdi As MainForm
Friend fChild As ChildForm

End Module

#End Region ' Declarations

#Region " ChildFroms "

Module ChildFroms

#Region " Delegates "

Delegate Sub AddChildDelegate(ByVal child As Form)

#End Region ' Delegates

Friend Sub InstanciateForm()
Dim Child As New ChildForm
Child.Text = "Do something"
Adding(Child)
End Sub

Public Sub Adding(ByVal Child As Form)
If fMdi.InvokeRequired Then
Dim AddChildDlg As New AddChildDelegate(AddressOf Adding)
fMdi.BeginInvoke(AddChildDlg, Child)
Else
Child.MdiParent = fMdi
Child.Show()
End If
End Sub


End Module

#End Region ' ChildFroms

#Region " ChildForm "

Public Class ChildForm

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
MsgBox("Hello World")
End Sub

Private Sub ChildForm_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
fChild = Me
End Sub

End Class

#End Region ' ChildForm

#Region " MainForm "

Imports System.Threading

Public Class MainForm

Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
fMdi = Me
Dim t1 As New Thread(New ThreadStart(AddressOf
ChildFroms.InstanciateForm))
t1.Start()
End Sub

End Class

#End Region ' MainForm


Regards,

Michael
 

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

Similar Threads

MDI Problem 3
Opening MDI childforms 4
Question on MDIchild forms 6
MDI Form Does not display Child Form if a Split Container used 1
PRoblem with MDI child form 3
MDI question 1
MDI form 1
MDI Validating 1

Top