// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.Collections.Generic; using System.Threading; using EE; namespace E { public class Person { public string Name { get; set; } } public class ResponsibleHolder { private static ResponsibleHolder instance = null; private static object mylock = new object(); private Dictionary responsible; private ResponsibleHolder() { responsible = new Dictionary(); } public static ResponsibleHolder Instance { get { lock(mylock) { if(instance == null) { instance = new ResponsibleHolder(); } } return instance; } } public Person Responsible { get { lock(responsible) { return responsible[Thread.CurrentThread.ManagedThreadId]; } } set { lock(responsible) { responsible[Thread.CurrentThread.ManagedThreadId] = value; } } } } public class ApplicationLogic { public void DoSomethings() { ResponsibleHolder.Instance.Responsible = new Person { Name="Arne" }; Foo foo = new Foo(); foo.A(); foo.B(); Bar bar = new Bar(); bar.C(); bar.D(); } } public class Program { public static void Main(string[] args) { ApplicationLogic app = new ApplicationLogic(); app.DoSomethings(); } } }