首次提交:添加src文件夹代码
This commit is contained in:
46
Cowain.Bake.Common/Core/MessageEventWaitHandle.cs
Normal file
46
Cowain.Bake.Common/Core/MessageEventWaitHandle.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Cowain.Bake.Common.Core
|
||||
{
|
||||
public class MessageEventWaitHandle<T> : EventWaitHandle
|
||||
{
|
||||
private T message;
|
||||
private readonly object lockEvent = new object();//定义锁
|
||||
public MessageEventWaitHandle(bool initialState, EventResetMode mode)
|
||||
: base(initialState, mode)
|
||||
{
|
||||
}
|
||||
|
||||
public bool Set(T message)
|
||||
{
|
||||
//lock (lockEvent)
|
||||
{
|
||||
this.message = message;
|
||||
return base.Set();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public T GetMessage(int timeOut)
|
||||
{
|
||||
//lock (lockEvent)//因为这里锁住了,set给不了信号
|
||||
{
|
||||
if (!base.WaitOne(timeOut)) //为假超时
|
||||
{
|
||||
base.Reset();
|
||||
return default(T);
|
||||
}
|
||||
else
|
||||
{
|
||||
base.Reset();
|
||||
return this.message;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user