📄 asyncfuture.cs
字号:
namespace NCindy.Session.AIO
{
using NCindy;
using NCindy.Util;
using NCindy.Util.Logging;
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Threading;
public sealed class AsyncFuture : IFuture
{
private object associatedObject;
private IPacket associatedPacket;
private IAsyncResult asyncResult;
private bool isCompleted;
private bool isSucceed;
private static readonly ILogger log = LogFactory.CreateLogger(MethodBase.GetCurrentMethod().ReflectedType);
private readonly ISession session;
private object tag;
private readonly List<WaitHandle> waitHandles;
public event EventHandler<FutureCompletedEventArgs> FutureCompleted;
public AsyncFuture(ISession session)
{
this.session = session;
this.isSucceed = false;
this.waitHandles = new List<WaitHandle>();
}
public void AddWaitHandle(WaitHandle waitHandle)
{
LangHelper.CheckNullArgument("waitHandle", waitHandle);
if (this.waitHandles.IndexOf(waitHandle) < 0)
{
this.waitHandles.Add(waitHandle);
}
}
public bool Complete()
{
return this.Complete(0x4e20);
}
public bool Complete(int timeout)
{
if (this.isCompleted)
{
return true;
}
if (this.waitHandles.get_Count() == 0)
{
return false;
}
try
{
return WaitHandle.WaitAll(this.waitHandles.ToArray(), timeout, true);
}
catch (AbandonedMutexException exception)
{
log.Error("The wait terminated because a thread exited without releasing a mutex.", exception);
return true;
}
catch (Exception exception2)
{
log.Error("Future complete failed.", exception2);
return false;
}
}
private void FireFutureCompletedEvent()
{
if (this.FutureCompleted != null)
{
try
{
this.FutureCompleted.Invoke(this, new FutureCompletedEventArgs(this));
}
catch
{
}
}
}
internal object AssociatedObject
{
get
{
return this.associatedObject;
}
set
{
this.associatedObject = value;
}
}
internal IPacket AssociatedPacket
{
get
{
return this.associatedPacket;
}
set
{
this.associatedPacket = value;
}
}
public IAsyncResult AsyncResult
{
get
{
return this.asyncResult;
}
set
{
if ((this.asyncResult == null) && (this.asyncResult != value))
{
this.asyncResult = value;
this.waitHandles.Add(this.asyncResult.AsyncWaitHandle);
}
}
}
public bool IsCompleted
{
get
{
if (this.asyncResult != null)
{
return this.asyncResult.IsCompleted;
}
return this.isCompleted;
}
set
{
if (this.asyncResult != null)
{
throw new InvalidOperationException();
}
this.isCompleted = value;
}
}
public bool IsSucceeded
{
get
{
return this.isSucceed;
}
set
{
this.isSucceed = value;
this.FireFutureCompletedEvent();
}
}
public ISession Session
{
get
{
return this.session;
}
}
public object Tag
{
get
{
return this.tag;
}
set
{
this.tag = value;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -