using System; namespace ScreenConnect; public class ContextHolder where TRunContext : RunContext { public object SyncLock { get; } = new object(); public TRunContext? CurrentContext { get; protected set; } public IDisposable WithNewContext(Func contextCreator, out TRunContext currentContext) { lock (SyncLock) { CurrentContext.AssertNull(); CurrentContext = contextCreator(); currentContext = CurrentContext; } return new ProcDisposable(delegate { lock (SyncLock) { TRunContext? currentContext2 = CurrentContext; CurrentContext = null; currentContext2?.Dispose(); } }); } }