Files
ICD.Common.Utils/ICD.Common.Utils/Threading/SafeCriticalSection.cs

52 lines
886 B
C#

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");
Enter();
try
{
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");
Enter();
try
{
return callback();
}
finally
{
Leave();
}
}
}
}