NGWS SDK Documentation  

This is preliminary documentation and subject to change.
To comment on this topic, please send us email at ngwssdk@microsoft.com. Thanks!

Thread Affinity Concepts

Key Points

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 context will execute on the same thread for the lifetime of the objects.

[ThreadAffinity()]
public class Foo : ContextBoundObject
{
. . . // implementation for Foo
}

// Make an instance of Foo
void SomeOtherMethod(void)
{
. . . Foo foo = new Foo();
}

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.

The finalizer for thread affinity marked components is run on the thread for the context i.e. the same one the object was created on when the constructor was called.

For reference details see: ThreadAffinity.