When to Set ThreadApartmentState when using Component that is exposed to VB6
I have an COM component that I am fixing at the moment that is used within a VB6 application (and Raises Events) to the hosting VB6 program.

This Component uses Multiple Threads to perform it's work internally and then moves the events out to the layer that is actually exposed to the VB6 application.

In the constructor of the Component that is called via the VB6 Application (New MyObject) this block of code within the component gets called 

    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 


within the threads they raiseevents that are received in this outer object.

the events eventually find their way into `CommsCommunicationsError`, which then does some tricks to get it onto the Correct thread for raising events into 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


Is this how is should be getting done, as I am sometimes seeing random crashes in the application at the point when one of these events would be raised.

Should the Threads have their apartmentstate changed to STA(maybe) instead of MTA(the default)?

When to Set ThreadApartmentState when using Component that is exposed to VB6
===

I have an COM component that I am fixing at the moment that is used within a VB6 application (and Raises Events) to the hosting VB6 program.

This Component uses Multiple Threads to perform it's work internally and then moves the events out to the layer that is actually exposed to the VB6 application.

In the constructor of the Component that is called via the VB6 Application (New MyObject) this block of code within the component gets called 

    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 


within the threads they raiseevents that are received in this outer object.

the events eventually find their way into `CommsCommunicationsError`, which then does some tricks to get it onto the Correct thread for raising events into 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


Is this how is should be getting done, as I am sometimes seeing random crashes in the application at the point when one of these events would be raised.

Should the Threads have their apartmentstate changed to STA(maybe) instead of MTA(the default)?
