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
 
Ad

Advertisements

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).
 
Ad

Advertisements

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
MDI forms and threads 1
MDI form 1
MDI-Forms with thread 7
elementHost repaint problem in MDI application 0
Forms/Threading problem 1
Closing MDI Child Form Won't Reopen? Help! 11
re mdi Apps 3

Top