/// <summary> /// 获取EventWaitHandle,获取成功返回获取EventWaitHandle;失败返回null /// </summary> /// <param name="id">EventWaitHandle的唯一标识</param> /// <returns>EventWaitHandle</returns> public static EventWaitHandle GetEventWaitHandle(object id) { EventWaitHandleInfo eventWaitHandleInfo = GetEventWaitHandleInfo(id); if (eventWaitHandleInfo != null) { return(eventWaitHandleInfo.EventWaitHandle); } return(null); }
/// <summary> /// 创建并添加EventWaitHandle /// </summary> /// <param name="id">EventWaitHandle的唯一标识</param> /// <param name="expirationMilliseconds">缓存项有效时间,小于等于0永不过期,单位/毫秒</param> /// <param name="tag">Tag</param> /// <returns>创建的EventWaitHandle</returns> public static EventWaitHandleInfo CreateEventWaitHandle(object id, int expirationMilliseconds = -1, object tag = null) { var eventWaitHandleInfo = new EventWaitHandleInfo(id, new AutoResetEvent(false), tag); if (_eventWaitHandleDic.TryAdd(id, eventWaitHandleInfo)) { AddIdToCache(id, expirationMilliseconds); return(eventWaitHandleInfo); } eventWaitHandleInfo.EventWaitHandle.Dispose(); throw new ArgumentException($"key{id}已存在"); }
/// <summary> /// 添加或更新外部创建的EventWaitHandle,如果id存在则替换并返回旧的项,否则则添加返回null /// </summary> /// <param name="id">EventWaitHandle的唯一标识</param> /// <param name="eventWaitHandle">EventWaitHandle</param> /// <param name="expiration">缓存项有效时间,小于等于0永不过期,单位/毫秒</param> /// <param name="tag">Tag</param> /// <returns>旧的EventWaitHandle</returns> public static EventWaitHandle AddOrUpdateEventWaitHandle(object id, EventWaitHandle eventWaitHandle, int expiration = -1, object tag = null) { EventWaitHandleInfo oldEventWaitHandleInfo = null; var newValue = new EventWaitHandleInfo(id, eventWaitHandle, tag); _eventWaitHandleDic.AddOrUpdate(id, newValue, (key, oldValue) => { oldEventWaitHandleInfo = oldValue; return(newValue); }); AddIdToCache(id, expiration); if (oldEventWaitHandleInfo != null) { return(oldEventWaitHandleInfo.EventWaitHandle); } else { return(null); } }