당신은 UI 스레드가 frmMain.refreshStats 메소드를 호출이 필요합니다. Control.InvokeRequired 속성과 Control.Invoke (MSDN Documentation)를 사용하면 여러 가지 방법으로이 작업을 수행 할 수 있습니다.
"EndAsync"메서드를 사용하면 메서드 호출 UI 스레드를 안전하게 만들거나 refreshStats 메서드에서 스레드 안전성을 검사하도록 할 수 있습니다 (Control.InvokeRequired 사용).
EndAsync UI 스레드 안전이 같은 것입니다 :
Public Delegate Sub Method(Of T1, T2)(ByVal arg1 As T1, ByVal arg2 As T2)
Sub skDataReceived(ByVal result As IAsyncResult)
Dim frmMain As Form = CType(My.Application.OpenForms.Item("frmMain"), frmMain)
Dim d As Method(Of Object, Object)
'create a generic delegate pointing to the refreshStats method
d = New Method(Of Object, Object)(AddressOf frmMain.refreshStats)
'invoke the delegate under the UI thread
frmMain.Invoke(d, New Object() {d1, d2})
End Sub
또는 당신은 UI 스레드에서 자신을 호출 할 필요가있는 경우 볼 수있는 refreshStats 방법을 확인 할 수 있습니다 :
Public Delegate Sub Method(Of T1, T2)(ByVal arg1 As T1, ByVal arg2 As T2)
Sub refreshStats(ByVal d1 As Object, ByVal d2 As Object)
'check to see if current thread is the UI thread
If (Me.InvokeRequired = True) Then
Dim d As Method(Of Object, Object)
'create a delegate pointing to itself
d = New Method(Of Object, Object)(AddressOf Me.refreshStats)
'then invoke itself under the UI thread
Me.Invoke(d, New Object() {d1, d2})
Else
'actual code that requires UI thread safety goes here
End If
End Sub
하는 경우를 나는 frmMain을 직접 호출하는데, 윈도우 핸들이 생성 된 후에 만 호출이 컨트롤에서 호출 될 수 있다는 오류가 발생합니다. 그래서 OpenForms 컬렉션에서 폼을 사용하려고했지만, 이전의 "The Undo operation ..."과 같은 오류가 발생했습니다. 무엇을 잘못하고 있습니까? –
즉, "해결책"은 내 기본 문제 인 InvalidContextException을 해결하지 않습니다. 어쨌든 누군가가 이것을 다시 만나면 나는 이것에 대한 답을 찾았습니다. 아래 내 대답을 참조하십시오. –
그래서 기본적으로 내가 생각하는 것은 Windows가 폼 창에 대한 Win32 핸들을 만들기 전에이 코드가 실행되고 있다는 것입니다. 나는이 코드가 실행되기 전에 윈도우가 보이는지 궁금하다. –