Re: Threads,Callbacks,Interop

Tech-Archive recommends: Fix windows errors by optimizing your registry



> If you make sure all your "interop stuff" is performed using on a STA
> thread
> you should only ever get events on this thread.

Not true in the managed world. In the managed world all of your objects
lack any thread affinity at all. It's best to think of them as living in
the Thread Neutral Apartment or the MTA and aggregating the free threaded
marshaller. It is not possible[0] to give your managed objects any kind of
thread affinity.

> Use the ProcessThread class or P/Invoke to get the thread ID.

P/Invoking to get the Thread ID can lead to some problems. .NET does not
gaurantee a 1-1 relationship with Managed and Native threads. It is
possible for one managed thread to be hosted by many different Native
Threads over time [1]. So P/Invoking for the ID can give you different
result for the same managed thread path.

Use Thread.GetHashCode() instead with the 1.x framework and
Thread.ManagedThreadId with 2.0.

[0] Not possible is probably too strong of a phrase here. You could do it
with a mighty set of hacks with controls and event pumping but that will
lead to other issues.
[1] I think there may be a gaurantee for the main STA thread but can't
gaurantee it.
--
Jared Parsons [MSFT]
jaredpar@xxxxxxxxxxxxxxxxxxxx
http://blogs.msdn.com/jaredpar
"This posting is provided "AS IS" with no warranties, and confers no rights"
"Martin Liversage" <martin@xxxxxxxxxxxxx> wrote in message
news:3463CFE3-13AC-43C8-816C-36D4507F0035@xxxxxxxxxxxxxxxx
> "JDR" wrote:
>
>> This stinks of thread issues to me.
>
> If you make sure all your "interop stuff" is performed using on a STA
> thread
> you should only ever get events on this thread. This means that you cannot
> recieve new events while processing an event since a thread can only do
> one
> task at a time. If using a STA thread is an option for you (see the
> ApartmentState enumeration) you could try to log the thread ID of the
> threads
> calling you back. Use the ProcessThread class or P/Invoke to get the
> thread
> ID. If you see "foreign" thread ID's in your log it is a telltale sign of
> threading problems in your telephony component. Then they are probably
> calling you back on threads from other apartments using unmarshalled
> pointers
> which is illegal. Incidentially I once worked with a customer that had
> this
> exact bug in a - yes, you guessed it - telephony component.
>
> If on the other hand you instantiate the telephony component in a MTA you
> should be prepared to recieve callbacks on multiple threads. I assume this
> can happen concurrently in a telephony application.
>
> --
> Martin Liversage
>


.



Relevant Pages

  • Re: Threads,Callbacks,Interop
    ... >> Use the ProcessThread class or P/Invoke to get the thread ID. ... > gaurantee a 1-1 relationship with Managed and Native threads. ... >> If on the other hand you instantiate the telephony component in a MTA you ...
    (microsoft.public.dotnet.framework.interop)
  • RE: Threads,Callbacks,Interop
    ... If you make sure all your "interop stuff" is performed using on a STA thread ... ApartmentState enumeration) you could try to log the thread ID of the threads ... calling you back on threads from other apartments using unmarshalled pointers ... exact bug in a - yes, you guessed it - telephony component. ...
    (microsoft.public.dotnet.framework.interop)