0

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
Blackie
  • 15
  • 1
  • 5
  • The fact that you're testing `InvokeRequired` and calling `BeginInvoke` in that code suggests that it will or might not be executed on the UI thread. That means that it should not be accessing ANY members of any forms or other controls in that code. The `Show` method of `aForm` is a member of a form, so it's off limits. You'll need to do a similar test and invoke to call `Show` on that form on the UI thread. – jmcilhinney Sep 30 '16 at 16:24

1 Answers1

1

You should/must1 Invoke code which accesses a control on the thread on which the control was created.

Don't mix up Control.Invoke and delegate.BeginInvoke. See What's the difference between Invoke() and BeginInvoke()

The line which raises the error could be changed to

aForm.Invoke(Sub() aForm.Show())

However, you might not always want to invoke, for instance when you are executing code on the proper thread already, the Invoke call is wasteful. That's why Controls have the InvokeRequired property. Here is a specific implementation of the invoke-if-invokerequired pattern for showing a form.

Private Sub showForm(ByVal f As Form)
    If f.InvokeRequired Then
        f.Invoke(New Action(Of Form)(AddressOf showForm), f)
    Else
        f.Show()
    End If
End Sub

' usage:
showForm(aForm)

If you are doing a lot of UI programming, you may find that you are writing these methods a lot. So you can automate the pattern

You can put this extension method in a module. It allows you to pass a delegate which does whatever you want, and it will be invoked on the control's thread if required

<Extension()> _
Public Sub InvokeIfRequired(ByVal control As Control, action As MethodInvoker)
    If control.InvokeRequired Then
        control.Invoke(action)
    Else
        action()
    End If
End Sub

' usage:
aForm.InvokeIfRequired(Sub() aForm.Show())

1 In some cases, accessing controls from the wrong thread will not raise an exception, but it can cause intermittent exceptions. In my experience it is non-deterministic. For example, retrieving TextBox.Text on the wrong thread is usually fine, but setting TextBox.Text will usually raise an exception. For this reason, it is good practice to use the invoke-if-invokerequired pattern whenever doing anything with controls outside of their own event handlers, or at least outside event handlers for controls on the same form.

Community
  • 1
  • 1
djv
  • 11,577
  • 7
  • 43
  • 62
  • I tried your first suggestion and this did away with the Cross-Thread exception, however, now the MDI Child cfrmOverview take a considerable amount of time to load. – Blackie Sep 30 '16 at 20:34
  • Well it wasn't loading before because of the cross-thread exception, correct? Maybe `cfrmOverview.cfrmOverview_Load()` is just doing a lot of things. – djv Sep 30 '16 at 21:40