I have an object that serves multiple requests (threads) … think of SqlConnection object across multiple threads ….
Now, I want to create a “Thread” safe object which is aware of the thread context in which it is created.
So if Thread1 creates object foo and Thread2 tries to access it …. object foo would ignore it and only “act” if Thread1 was running …
Will ContextBoundObject do this? if yes, what are its limitations?
Pseudo code
public class Foo
{
private int _threadId;
public void DoSomething()
{
(if thread.ManagedThreadId != _threadId) return;
// Do some thread safe stuff
}
}
Behavior you want to achieve can be made by using SynchronizationAttribute.
However, using this attribute will serialize access to all members of the class. On the other hand if some methods of the class do not require synchronization (like static method that do not share any state) using Synchronization attribute may result in degraded performance.