Skip to content
This repository has been archived by the owner on Apr 14, 2023. It is now read-only.

uhm0311/SimpleThreadMonitor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SimpleThreadMonitor

SimpleThreadMonitor provides simple mutex feature.

using SimpleThreadMonitor;

object lock = new object();

SimpleMutex.Lock(lock, () => 
{
  // Do something
});

Implementaion

public static class SimpleMutex
{
  public static void Lock(object Object, Action Process, Action<Exception> ExceptionCallback = null, bool ReleaseLockBeforeExceptionCallback = false)
  {
    if (Object != null && Process != null)
    {
      try
      {
        Monitor.Enter(Object);
        {
          Process();
        }
        Monitor.Exit(Object);
      }
      catch (Exception Exception)
      {
        if (ReleaseLockBeforeExceptionCallback)
        {
          Monitor.Exit(Object);
        }

        try { ExceptionCallback?.Invoke(Exception); }
        catch { }
        
        if (!ReleaseLockBeforeExceptionCallback)
        {
          Monitor.Exit(Object);
        }
      }
    }
  }
}

Note

Since this project is only for simple mutex, there is NO plan to provide any other complex features.

About

Simple C# Mutex.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages