我有一個COM組件,我正在修復在VB6應用程序(並引發事件)內使用的託管VB6程序。何時使用暴露給VB6的組件設置ThreadApartmentState
該組件使用多個線程在內部執行它的工作,然後將事件移出到實際暴露給VB6應用程序的圖層。
在組件的構造,其經由VB6應用程序(新的MyObject)的部件內代碼塊稱爲獲取調用
public sub New()
mSyncContext = System.Threading.SynchronizationContext.Current
If mSyncContext Is Nothing Then
Using f As New Windows.Forms.Form
mSyncContext = System.Threading.SynchronizationContext.Current
End Using
End If
end sub
了在該外部物體接收他們raiseevents螺紋內。
事件最終會找到CommsCommunicationsError
的方法,然後它會執行一些技巧將其引入到正確的線程中,以便將事件引發到VisualBasic6中。
Private Sub CommsCommunicationsError(ByVal theErrorNumber As Integer, ByVal theOrder As Order)
mRecordingCounter += 1
Dim args As OrderErrorEventArgs
If theOrder.Parent IsNot Nothing Then
args = New OrderErrorEventArgs(theErrorNumber, theOrder.Parent, mRecordingCounter)
Else
args = New OrderErrorEventArgs(theErrorNumber, theOrder, mRecordingCounter)
End If
PostToCommunicationsError(args)
End Sub
Private Sub PostToCommunicationsError(ByVal args As OrderErrorEventArgs)
mSyncContext.Post(AddressOf CommunicationsErrorSend, args)
End Sub
Private Sub CommunicationsErrorSend(ByVal state As Object)
Dim args As OrderErrorEventArgs = CType(state, OrderErrorEventArgs)
onCommunicationsError(args)
End Sub
Private Sub onCommunicationsError(ByVal args As OrderErrorEventArgs)
RaiseEvent CommunicationsError(args.ErrorNumber, args.Order)
End Sub
請問這是怎麼應該得到實現,因爲我有時會看到隨機崩潰在該點的應用程序時,這些事件的人會得到提升。
線程是否應將其狀態改爲STA(也許)而不是MTA(默認值)?
不幸的是,在這種情況下,我不是在尋找一個可能的,因爲我已經在可能的陣營,希望有更多的defintive –