SyncSignal
项目组其他成员写的code,即将被删掉,贴在这儿纪念一下使用AutosetEvent来进行同步的方法。
SyncSignalCollection.cs
代码
public class SessionSyncSignalCollection
{
private SynchronisedDictionary<string, SessionSyncSignal> innerDict;
private readonly object syncRoot = new object();
public SessionSyncSignalCollection()
{
this.innerDict = new SynchronisedDictionary<string, SessionSyncSignal>();
}
public void AddSignal(string egmId, string cardId)
{
lock (syncRoot)
{
SessionSyncSignal signal = FindSignal(egmId, cardId);
if (signal == null)
{
signal = new SessionSyncSignal();
innerDict.Add(SessionStateHelper.GetHashKey(egmId, cardId), signal);
}
else
{
signal.Add();
}
}
}
public bool TryDelSignal(string egmId, string cardId)
{
lock (syncRoot)
{
SessionSyncSignal signal = FindSignal(egmId, cardId);
if (signal != null)
{
signal = new SessionSyncSignal();
innerDict.Remove(SessionStateHelper.GetHashKey(egmId, cardId));
return true;
}
}
return false;
}
public bool TrySetSignal(string egmId, string cardId)
{
lock (syncRoot)
{
SessionSyncSignal signal = FindSignal(egmId, cardId);
if (signal != null)
{
signal.Set();
return true;
}
}
return false;
}
public bool WaitSignal(string egmId, string cardId)
{
bool isSucessful = false;
SessionSyncSignal signal;
lock (syncRoot)
{
signal = FindSignal(egmId, cardId);
}
if (signal != null)
{
isSucessful = signal.Wait();
lock (syncRoot)
{
if (signal.CanCleared)
{
innerDict.Remove(SessionStateHelper.GetHashKey(egmId, cardId));
}
}
}
return isSucessful;
}
private SessionSyncSignal FindSignal(string egmId, string cardId)
{
string key = SessionStateHelper.GetHashKey(egmId, cardId);
SessionSyncSignal signal = null;
innerDict.TryGetValue(key, out signal);
return signal;
}
}
{
private SynchronisedDictionary<string, SessionSyncSignal> innerDict;
private readonly object syncRoot = new object();
public SessionSyncSignalCollection()
{
this.innerDict = new SynchronisedDictionary<string, SessionSyncSignal>();
}
public void AddSignal(string egmId, string cardId)
{
lock (syncRoot)
{
SessionSyncSignal signal = FindSignal(egmId, cardId);
if (signal == null)
{
signal = new SessionSyncSignal();
innerDict.Add(SessionStateHelper.GetHashKey(egmId, cardId), signal);
}
else
{
signal.Add();
}
}
}
public bool TryDelSignal(string egmId, string cardId)
{
lock (syncRoot)
{
SessionSyncSignal signal = FindSignal(egmId, cardId);
if (signal != null)
{
signal = new SessionSyncSignal();
innerDict.Remove(SessionStateHelper.GetHashKey(egmId, cardId));
return true;
}
}
return false;
}
public bool TrySetSignal(string egmId, string cardId)
{
lock (syncRoot)
{
SessionSyncSignal signal = FindSignal(egmId, cardId);
if (signal != null)
{
signal.Set();
return true;
}
}
return false;
}
public bool WaitSignal(string egmId, string cardId)
{
bool isSucessful = false;
SessionSyncSignal signal;
lock (syncRoot)
{
signal = FindSignal(egmId, cardId);
}
if (signal != null)
{
isSucessful = signal.Wait();
lock (syncRoot)
{
if (signal.CanCleared)
{
innerDict.Remove(SessionStateHelper.GetHashKey(egmId, cardId));
}
}
}
return isSucessful;
}
private SessionSyncSignal FindSignal(string egmId, string cardId)
{
string key = SessionStateHelper.GetHashKey(egmId, cardId);
SessionSyncSignal signal = null;
innerDict.TryGetValue(key, out signal);
return signal;
}
}
SyncSignal.cs
代码
public class SessionSyncSignal
{
// the counter for cardIn events
private int eventCounter;
// the counter for PlayerSessionStart events
private int signalCounter;
private AutoResetEvent syncEvent { get; set;}
private const int waitMilliseconds = 30000;
public bool CanCleared
{
get
{
return eventCounter == 0;
}
}
public SessionSyncSignal()
{
syncEvent = new AutoResetEvent(false);
signalCounter = 0;
eventCounter = 1;
}
public void Add()
{
eventCounter++;
}
public void Set()
{
if (signalCounter < 0)
{
syncEvent.Set();
}
signalCounter++;
}
public bool Wait()
{
signalCounter--;
eventCounter--;
bool isSignalGot = true;
if (signalCounter < 0)
{
isSignalGot = syncEvent.WaitOne(waitMilliseconds);
}
return isSignalGot;
}
}
{
// the counter for cardIn events
private int eventCounter;
// the counter for PlayerSessionStart events
private int signalCounter;
private AutoResetEvent syncEvent { get; set;}
private const int waitMilliseconds = 30000;
public bool CanCleared
{
get
{
return eventCounter == 0;
}
}
public SessionSyncSignal()
{
syncEvent = new AutoResetEvent(false);
signalCounter = 0;
eventCounter = 1;
}
public void Add()
{
eventCounter++;
}
public void Set()
{
if (signalCounter < 0)
{
syncEvent.Set();
}
signalCounter++;
}
public bool Wait()
{
signalCounter--;
eventCounter--;
bool isSignalGot = true;
if (signalCounter < 0)
{
isSignalGot = syncEvent.WaitOne(waitMilliseconds);
}
return isSignalGot;
}
}