Files
6098/Cowain.Bake.Common/Core/MessageEventWaitHandle.cs

47 lines
1.1 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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;
}
}
}
}
}