47 lines
1.1 KiB
C#
47 lines
1.1 KiB
C#
|
|
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;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|