Hi Sidd,
Here I provide a sample Semaphore implementation using Win32 API. I hope it
will be helpful to you.
usage:
Semaphore s = new Semaphore(5, true);
s.Wait();
//a maximum of 5 threads will be allowed in this section
s.Signal();
//<code>
using System.Runtime.InteropServices;
namespace System
{
[StructLayout(LayoutKind.Sequential)]
internal struct SECURITY_ATTRIBUTES
{
public int nLength;
public IntPtr lpSecurityDescriptor;
public bool bInheritHandle;
}
internal enum WaitResult
{
WAIT_ABANDONED = 0x00000080,
WAIT_IO_COMPLETION = 0x000000C0,
WAIT_OBJECT_0 = 0x00000000,
WAIT_TIMEOUT = 0x00000102
}
/// <summary>
/// Summary description for Semaphore.
/// </summary>
public class Semaphore : IDisposable
{
private IntPtr hSemaphore;
[DllImport("kernel32.dll", SetLastError=true)]
private static extern IntPtr CreateSemaphore(ref SECURITY_ATTRIBUTES
securityAttributes, int initialCount, int maximumCount, string name);
[DllImport("kernel32.dll")]
private static extern bool ReleaseSemaphore(IntPtr hSemaphore, int
lReleaseCount, IntPtr lpPreviousCount);
[DllImport("kernel32.dll", SetLastError=true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool CloseHandle(IntPtr hObject);
[DllImport("kernel32", SetLastError=true, ExactSpelling=true)]
private static extern Int32 WaitForSingleObject(IntPtr handle, Int32
milliseconds);
private static uint INFINITE = 0xFFFFFFFF;
/// <summary>
/// Creates a semaphore initialized in it's nonsignalled state
/// </summary>
/// <param name="maxCount">maximum number of threads</param>
public Semaphore(int maxCount) : this(maxCount, false)
{
}
public Semaphore(int maxCount, bool initiallySignalled) :
this(initiallySignalled ? maxCount : 0, maxCount)
{
}
public Semaphore(int initialCount, int maxCount) : this(initialCount,
maxCount, null)
{
}
protected Semaphore(int initialCount, int maxCount, string name)
{
if (maxCount < 0 || initialCount < 0)
{
throw new ArgumentException("initialCount and maximumCount must not be
negative");
}
if (initialCount > maxCount)
{
throw new ArgumentException("initialCount must not be more than
maximumCount", "initialCount");
}
SECURITY_ATTRIBUTES attr = new SECURITY_ATTRIBUTES();
hSemaphore = CreateSemaphore(ref attr, initialCount, maxCount, name);
if (hSemaphore == IntPtr.Zero)
{
int error = System.Runtime.InteropServices.Marshal.GetLastWin3 2Error();
throw new ApplicationException("Semaphore could not be created, error
code: " + error);
}
}
public void Dispose()
{
try
{
CloseHandle(hSemaphore);
}
finally
{
hSemaphore = IntPtr.Zero;
}
}
public void Wait()
{
int o = WaitForSingleObject(hSemaphore, (int)INFINITE);
WaitResult r = (WaitResult)o;
}
public void Signal()
{
if (!ReleaseSemaphore(hSemaphore, 1, IntPtr.Zero))
{
int error = System.Runtime.InteropServices.Marshal.GetLastWin3 2Error();
if (error == 0)
{
throw new ApplicationException("Semaphore was already in it's signalled
state");
}
else
{
throw new ApplicationException("Semaphore could not be released, error
code: " + error);
}
}
}
}
}
//</code>