.net - When to Set ThreadApartmentState when using Component that is exposed to VB6 -
i have com component fixing @ moment used within vb6 application (and raises events) hosting vb6 program.
this component uses multiple threads perform it's work internally , moves events out layer exposed vb6 application.
in constructor of component called via vb6 application (new myobject) block of code within component gets called
public sub new() msynccontext = system.threading.synchronizationcontext.current if msynccontext nothing using f new windows.forms.form msynccontext = system.threading.synchronizationcontext.current end using end if end sub within threads raiseevents received in outer object.
the events find way commscommunicationserror, tricks onto correct thread raising events visualbasic6.
private sub commscommunicationserror(byval theerrornumber integer, byval theorder order) mrecordingcounter += 1 dim args ordererroreventargs if theorder.parent isnot nothing 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 ordererroreventargs) msynccontext.post(addressof communicationserrorsend, args) end sub private sub communicationserrorsend(byval state object) dim args ordererroreventargs = ctype(state, ordererroreventargs) oncommunicationserror(args) end sub private sub oncommunicationserror(byval args ordererroreventargs) raiseevent communicationserror(args.errornumber, args.order) end sub is how should getting done, seeing random crashes in application @ point when 1 of these events raised.
should threads have apartmentstate changed sta(maybe) instead of mta(the default)?
yes, maybe the threads should sta
Comments
Post a Comment