mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-16 05:05:05 +00:00
Created S# .sln and moved project to src folder
This commit is contained in:
49
ICD.Common/Utils/SafeCriticalSection.cs
Normal file
49
ICD.Common/Utils/SafeCriticalSection.cs
Normal file
@@ -0,0 +1,49 @@
|
||||
using System;
|
||||
|
||||
namespace ICD.Common.Utils
|
||||
{
|
||||
public sealed partial class SafeCriticalSection
|
||||
{
|
||||
/// <summary>
|
||||
/// Enters the critical section, executes the callback and leaves the section.
|
||||
/// </summary>
|
||||
/// <param name="callback"></param>
|
||||
public void Execute(Action callback)
|
||||
{
|
||||
if (callback == null)
|
||||
throw new ArgumentNullException("callback");
|
||||
|
||||
try
|
||||
{
|
||||
Enter();
|
||||
callback();
|
||||
}
|
||||
finally
|
||||
{
|
||||
Leave();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Enters the critical section, executes the callback and leaves the section.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="callback"></param>
|
||||
/// <returns></returns>
|
||||
public T Execute<T>(Func<T> callback)
|
||||
{
|
||||
if (callback == null)
|
||||
throw new ArgumentNullException("callback");
|
||||
|
||||
try
|
||||
{
|
||||
Enter();
|
||||
return callback();
|
||||
}
|
||||
finally
|
||||
{
|
||||
Leave();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user