It has been like never since I worked on Winform applications. My specialty has been working with Asp.net/Websites. Recently, I was given a application to upgrade from .Net 1.1 to .Net 4.6. The application is a MDI app in which it is having issues with a Cross-thread operations. Specifically, when a button event within a user-control is clicked the intent is to show the Main MDIchild form (cfrmOverview), however an error occurs because accessed to a Picturebox control called picDisplay is prevented. Even with the added code, I am still getting the error. I opted out of using CheckForIllegalCrossThreadCalls because it affects other parts the program and MSDN suggest not too. Insight needed.
Public Delegate Sub Mydelegate(ByVal AControl As PictureBox)
Public Shared Sub CreateEnableControl(ByVal AControl As PictureBox)
AControl.Visible = True
AControl.Enabled = True
End Sub
Public Shared Sub NavigateTo(ByVal sender As System.Windows.Forms.UserControl, ByVal aNavTarget As String, Optional ByVal param As Object = Nothing)
Dim aType As Type
Dim Types() As Type
Dim aobject As Object
Try
If IsNothing(System.Reflection.Assembly.GetEntryAssembly) Then
aobject = sender.ParentForm
Types = System.Reflection.Assembly.GetAssembly(aobject.GetType).GetTypes
Else
Types = System.Reflection.Assembly.GetEntryAssembly.GetTypes
End If
Dim aForm As Windows.Forms.Form
For Each aType In Types
If aType.BaseType Is GetType(MdiChild) Then
If aType.Name = aNavTarget Then
Dim aMdiParent As Windows.Forms.Form
If TypeOf (sender.ParentForm) Is MdiParent Then
aMdiParent = sender.ParentForm
Else
aMdiParent = sender.ParentForm.ParentForm
End If
For Each aForm In aMdiParent.MdiChildren
If aType.FullName Is aForm.GetType.FullName Then
aForm.Tag = param
'Added Code below to try to prevent Cross-Thread exception on PicDisplay found in the Main cfrmOverview Form
'that has designed time user control embedded.
'New Code Start----------------------------------------------------------------------
For Each aControl As Windows.Forms.Control In aForm.Controls.Find("picDisplay", True)
If aControl.InvokeRequired Then
Dim myArray(0) As Object
myArray(0) = New PictureBox
aControl.BeginInvoke(New Mydelegate(AddressOf CreateEnableControl), myArray)
End If
Next
'New Code End------------------------------------------------------------------------
aForm.Show() 'Cross-thread exception for picDisplay is here.
GoTo Success
End If