Key Points:
Refinement of Synchronized Context
Mark type with Thread Affinity Context Attribute
Only a single thread allowed to execute within the Synchronization Domain, hence only that thread can access instance fields and methods
Useful:
User Interface objects
Objects that hold onto OS resource that should only be accessed on the thread that created them
The Thread Affinity Context Property provides a similar capability as the Synchronization Context property with the further constraint that only a single thread is allow to execute within the context or synchronization domain at any time. This is useful for objects that could hold onto OS resources that should only be accessed from a particular thread and only that thread. Thread Affinity is useful when users want to always access the object on the same thread. Certain UI resources should only be accessed on the same thread, on which they were created.
An instance of this property in a context enforces a thread affinity domain for the context (and all contexts that share the same instance). This means that for all contexts sharing an instance of this property, all methods on objects resident in those contexts will execute on the same thread for the lifetime of the objects.
This is done by contributing message sinks that intercept and serialize in-coming calls.
If the property is marked for reentrancy, then call-outs are intercepted too. The call-out interception allows other waiting calls to enter the thread affinity domain.
[ThreadAffinity()]
public class Foo: ContextBoundObject
{
. . .// implementation for Foo
}
// Make an instance of Foo
void SomeOtherMethod(void)
{
. . . Foo foo = new Foo();
}
Object
Attribute
ContextAttribute
ThreadAffinity
[Visual Basic] Public Class ThreadAffinity Inherits ContextAttribute Implements IContributeServerContextSink, _ IContributeClientContextSink [C#] public class ThreadAffinity : ContextAttribute, IContributeServerContextSink, IContributeClientContextSink [C++] public __gc class ThreadAffinity : public ContextAttribute, IContributeServerContextSink, IContributeClientContextSink [JScript] public class ThreadAffinity extends ContextAttribute, IContributeServerContextSink, IContributeClientContextSink
[To be supplied.]
Namespace: System.Runtime.Remoting
Assembly: mscorlib.dll