[quoted text, click to view] "JB" <JB@discussions.microsoft.com> wrote in message
news:6FB73C54-66E7-4C34-85B7-4946D6CCC44D@microsoft.com...
> Stand down
>
> I changed the registration of the .ocx from
> Threadingmodel = Apartment
> to
> Threadingmodel = Both
> and that fixed the problem
> --
> JB
>
No, this is not a solution, by doing this you are fooling the system by
pretending that the component can be loaded into the MTA and COM will load
it into the newly created thread which enters the MTA by default, but beware
you are fooling yourself, here is why:
An OCX is not designed to be hosted in the MTA, because:
- OCX's are not thread safe, their accesses must be serialized, which is
automatically done by instantiating them in an STA thread.
- OCX's must be hosted in a ActiveX control container, an MTA is not such
container, the UI thread running a Windows Form is a valid container.
What you should do is:
1. set the Threadingmodel back to apartment and initialize your thread to
enter an STA by setting the ApartmentState attribute to STA2.
2. you'll need to pump the message queue, else you will have problems when
finalizing the COM component when you are done using it. Running a message
pump is done automatically when you create a Form and call Application.Run.
Willy.
..
[quoted text, click to view] >
> "JB" wrote:
>
>> Vb.net app with COM control. The COM control is an object framer used
>> by
>> the vb.net app to treat other apps( eg. word) as embedded objects.
>> Generally
>> this all works fine. One of the functions of this COM control is to
>> search
>> embedded app data for text ( 'Find' this or that.) This also works ok
>> in
>> its orginal form. I would like to run the search in the background
>> because
>> in some cases, it will search through many files and the problem comes
>> when I
>> try to run the search on a separate thread. In this case, I create a
>> thread
>> and in the thread proc, create the com control. crtl = New MyComCtrl
>> seems
>> to execute ok but if I then try ctrl.SomeMethod, I get an
>> ExecutionEngineException , First chance exception at Ox.... in MyApp.exe:
>> Access violation reading location 0x...-- Again, the same code works if
>> I
>> don't try to run it on a separate thread.
>>
>> It there some way to get around this problem? I have tried creating the
>> control before creating the tread and then calling the control inside the
>> threadproc but got the same error. Maybe I need to do some explicit
>> initialization of the .ocx in the thread??
>>
>> JB