Winform 응용 프로그램을 작성한 이후로는 결코 그렇지 않았습니다. 나의 전문 분야는 Asp.net/Websites와 협력 해왔다. 최근에 .Net 1.1에서 .Net 4.6으로 업그레이드하는 응용 프로그램이 제공되었습니다. 응용 프로그램은 크로스 스레드 작업에 문제가있는 MDI 응용 프로그램입니다. 특히 사용자 정의 컨트롤 내의 버튼 이벤트를 클릭하면 주 MDIchild 폼 (cfrmOverview)이 표시되지만 picDisplay라는 Picturebox 컨트롤에 액세스하면 오류가 발생하기 때문에 오류가 발생합니다. 추가 된 코드를 사용해도 여전히 오류가 발생합니다. CheckForIllegalCrossThreadCalls은 프로그램의 다른 부분에도 영향을 미치고 MSDN은 그렇지 않기 때문에 사용하지 않기로 결정했습니다. 통찰력이 필요했습니다.usercontrol .net 1.1 ~ 4.6의 단추를 통해 부모와 자식간에 MDI 크로스 스레드 예외가 발생했습니다.
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
'InvokeRequired'를 테스트하고 그 코드에서'BeginInvoke'를 호출하면 UI 스레드에서 실행될 수도 있고 실행되지 않을 수도 있습니다. 즉, 해당 코드의 양식이나 다른 컨트롤의 구성원은 액세스하지 않아야합니다. 'aForm'의'Show' 메쏘드는 폼의 멤버이므로, 제한이 없습니다. 비슷한 테스트를 수행하고 UI 스레드에서 해당 폼에 대해 'Show'를 호출하도록 호출해야합니다. – jmcilhinney