一直在考虑SharedMemory/SharedMemoryEx的并发问题..就是怎么处理同时有多个线程/进程读/写时怎么保证内容是正确的..
想到了Mutex,进程间的互斥体.
看了看MSDN,发现不能在多个进程中进行互斥操作:)
于是使用P/Invoke大法,封装了Win32中的OpenMutex/CreateMutex等类.写了一个MutexEx.
代码如下:
代码下载 : MutexEx.zip
想到了Mutex,进程间的互斥体.
看了看MSDN,发现不能在多个进程中进行互斥操作:)
于是使用P/Invoke大法,封装了Win32中的OpenMutex/CreateMutex等类.写了一个MutexEx.
代码如下:
Enum MutexExAccessTypes#region Enum MutexExAccessTypes
public enum MutexExAccessTypes:int
{
MUTEX_ALL_ACCESS = 0x1F0001,
MUTEX_MODIFY_STATE = 0x1,
}
#endregion
Class MutexEx#region Class MutexEx
/**//// <summary>
/// Summary description for Win32Mutex.
/// </summary>
public class MutexEx : IDisposable
{
DllImport#region DllImport
[DllImport("kernel32.dll")]
static extern IntPtr CreateMutex(IntPtr lpMutexAttributes, bool bInitialOwner,
string lpName);
[DllImport("kernel32.dll")]
static extern IntPtr OpenMutex(uint dwDesiredAccess, bool bInheritHandle,
string lpName);
[DllImport("kernel32.dll")]
static extern bool ReleaseMutex(IntPtr hMutex);
[DllImport("kernel32.dll", SetLastError=true)]
static extern int CloseHandle(IntPtr hObject);
[DllImport("kernel32.dll")]
static extern uint WaitForSingleObject(IntPtr hHandle, uint dwMilliseconds);
#endregion
Private varialbe & Propery#region Private varialbe & Propery
protected const uint INFINITE = 0xFFFFFFFF;
private string name;
private bool bOwner;
private IntPtr hMutex;
#endregion
Constructor & Dispose#region Constructor & Dispose
public MutexEx(){}
public MutexEx(string Name):this(Name,false){}
public MutexEx(string Name,bool bInitialOwner)
{
Create(Name,bInitialOwner);
}
public void Close()
{
if( hMutex != IntPtr.Zero)
{
CloseHandle(hMutex);
hMutex = IntPtr.Zero;
}
}
public void Dispose()
{
Close();
}
#endregion
Common Function#region Common Function
public void Create(string Name)
{
Create(Name,false);
}
public void Create(string Name,bool bInitialOwner)
{
name = Name;
bOwner = bInitialOwner;
hMutex = CreateMutex(IntPtr.Zero,bInitialOwner,Name);
if( hMutex == IntPtr.Zero)
{
throw new ApplicationException("Can't Create Mutex " + Name);
}
}
public void Open(string Name)
{
Open(Name,false,MutexExAccessTypes.MUTEX_ALL_ACCESS);
}
public void Open(string Name,MutexExAccessTypes Access)
{
Open(Name,false,Access);
}
public void Open(string Name,bool bInitialOwner,MutexExAccessTypes Access)
{
name = Name;
bOwner = bInitialOwner;
hMutex = OpenMutex( (uint)Access,bInitialOwner,Name);
if( hMutex == IntPtr.Zero)
{
throw new ApplicationException("Can't Open Mutex " + Name);
}
}
public uint Wait()
{
return Wait(INFINITE);
}
public uint Wait(uint dwMilliseconds)
{
return WaitForSingleObject(hMutex,dwMilliseconds);
}
public void Release()
{
ReleaseMutex(hMutex);
}
#endregion
}
#endregion
public enum MutexExAccessTypes:int
{
MUTEX_ALL_ACCESS = 0x1F0001,
MUTEX_MODIFY_STATE = 0x1,
}
#endregion
Class MutexEx#region Class MutexEx
/**//// <summary>
/// Summary description for Win32Mutex.
/// </summary>
public class MutexEx : IDisposable
{
DllImport#region DllImport
[DllImport("kernel32.dll")]
static extern IntPtr CreateMutex(IntPtr lpMutexAttributes, bool bInitialOwner,
string lpName);
[DllImport("kernel32.dll")]
static extern IntPtr OpenMutex(uint dwDesiredAccess, bool bInheritHandle,
string lpName);
[DllImport("kernel32.dll")]
static extern bool ReleaseMutex(IntPtr hMutex);
[DllImport("kernel32.dll", SetLastError=true)]
static extern int CloseHandle(IntPtr hObject);
[DllImport("kernel32.dll")]
static extern uint WaitForSingleObject(IntPtr hHandle, uint dwMilliseconds);
#endregion
Private varialbe & Propery#region Private varialbe & Propery
protected const uint INFINITE = 0xFFFFFFFF;
private string name;
private bool bOwner;
private IntPtr hMutex;
#endregion
Constructor & Dispose#region Constructor & Dispose
public MutexEx(){}
public MutexEx(string Name):this(Name,false){}
public MutexEx(string Name,bool bInitialOwner)
{
Create(Name,bInitialOwner);
}
public void Close()
{
if( hMutex != IntPtr.Zero)
{
CloseHandle(hMutex);
hMutex = IntPtr.Zero;
}
}
public void Dispose()
{
Close();
}
#endregion
Common Function#region Common Function
public void Create(string Name)
{
Create(Name,false);
}
public void Create(string Name,bool bInitialOwner)
{
name = Name;
bOwner = bInitialOwner;
hMutex = CreateMutex(IntPtr.Zero,bInitialOwner,Name);
if( hMutex == IntPtr.Zero)
{
throw new ApplicationException("Can't Create Mutex " + Name);
}
}
public void Open(string Name)
{
Open(Name,false,MutexExAccessTypes.MUTEX_ALL_ACCESS);
}
public void Open(string Name,MutexExAccessTypes Access)
{
Open(Name,false,Access);
}
public void Open(string Name,bool bInitialOwner,MutexExAccessTypes Access)
{
name = Name;
bOwner = bInitialOwner;
hMutex = OpenMutex( (uint)Access,bInitialOwner,Name);
if( hMutex == IntPtr.Zero)
{
throw new ApplicationException("Can't Open Mutex " + Name);
}
}
public uint Wait()
{
return Wait(INFINITE);
}
public uint Wait(uint dwMilliseconds)
{
return WaitForSingleObject(hMutex,dwMilliseconds);
}
public void Release()
{
ReleaseMutex(hMutex);
}
#endregion
}
#endregion
代码下载 : MutexEx.zip