私はWinformアプリケーションで作業して以来、私の専門はAsp.net /ウェブサイトで働いています。最近、.Net 1.1から.Net 4.6にアップグレードするためのアプリケーションが提供されました。アプリケーションは、クロススレッド操作で問題が発生しているMDIアプリケーションです。具体的には、ユーザーコントロール内のボタンイベントをクリックすると、メインMDIchildフォーム(cfrmOverview)が表示されますが、picDisplayというPictureboxコントロールにアクセスするとエラーが発生するため、エラーが発生します。追加されたコードがあっても、私はまだエラーが発生しています。 CheckForIllegalCrossThreadCallsを使用しないようにしました。これは、プログラムやMSDNでも他の部分に影響するためです。洞察力が必要です。親コントロールと子コントロールのMDIクロススレッド例外usercontrol.net 1.1から4.6へのボタン
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