1

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)?

Paul Farry
  • 4,730
  • 2
  • 35
  • 61

2 Answers2

0

Yes, maybe the threads should be STA

Community
  • 1
  • 1
MarkJ
  • 30,070
  • 5
  • 68
  • 111
  • Unfortunately in this case i'm not looking for a maybe, because i'm already in the maybe camp, hoping for something more definative – Paul Farry Jan 28 '11 at 03:50
0

I found that for the solution to this problem there was no requirement to alter the threading model. The issues had nothing to do with multi-threading. It was the networking code that was running behind the scenes in it's own thread that was creating exceptions and causing the main thread to fail (even though it was not supposed to)

Removing the Context Posting code consistently caused errors (of a different nature) that looked like the same thing, but they were consistent rather than random like the network code.

IMO I would recommend leaving threading apartments alone (use the defaults).

Paul Farry
  • 4,730
  • 2
  • 35
  • 61