Lock and unlock resources with single command <p>I am working with threads and that's the reason I use mutexes for locking shared resources. The base usage of locking is to put resources within Lock/unlock block.</p> <pre><code>procedure RefreshData; begin DataLock; GetData; GetSettings; CheckValues; ... DataUnlock; end; </code></pre> <p>Because there is always a pair Lock/Unlock I started thinking about simplified lock/unlock approach which would automatical unlock resources when not needed anymore.</p> <p>So my idea was to introduce new procedure which would take as an input parameter a reference to precedure. This will give me ability to use anonymous method.</p> <p>Code would be something like:</p> <pre><code>type TBaseProc = reference to procedure; procedure TMyObject.LockMethod(AMeth: TBaseProc); begin DataLock; try AMeth; finally DataUnlock; end; end; procedure TForm1.RefreshData; begin MyObject.LockMethod( procedure begin GetData; GetSettings; CheckValues; ... end; ); end; </code></pre> <p>Has this approach any sense or is there better or even easier solution to this? </p> <p>Thanks and regards.</p>