📄 defaultfuture.cs
字号:
namespace NCindy.Session
{
using NCindy;
using NCindy.Session.Dispatcher;
using NCindy.Util;
using System;
using System.Runtime.CompilerServices;
using System.Threading;
public class DefaultFuture : IFuture
{
private volatile bool completed;
private readonly ManualResetEvent completedEventWaitHandler;
private readonly IDispatcher dispatcher;
private readonly ISession session;
private volatile bool succeeded;
public event EventHandler<FutureCompletedEventArgs> FutureCompleted;
public DefaultFuture()
{
}
public DefaultFuture(ISession session)
{
this.dispatcher = DispatcherFactory.GetDispatcher();
this.completedEventWaitHandler = new ManualResetEvent(false);
this.session = session;
}
public DefaultFuture(ISession session, bool succeeded) : this(session)
{
this.completed = true;
this.succeeded = succeeded;
}
public bool Complete()
{
if (!this.completed)
{
lock (this)
{
while (!this.completed)
{
this.dispatcher.Block();
try
{
return this.completedEventWaitHandler.WaitOne(0x4e20, true);
}
catch (ThreadInterruptedException)
{
continue;
}
}
}
}
return this.succeeded;
}
public bool Complete(int timeout)
{
if (timeout < 0)
{
throw new ArgumentException();
}
if (!this.completed)
{
ElapsedTime time = new ElapsedTime();
lock (this)
{
while (!this.completed)
{
long num = timeout - time.GetElapsedTime();
if (num <= 0)
{
goto Label_0064;
}
this.dispatcher.Block();
try
{
this.completedEventWaitHandler.WaitOne(timeout, true);
continue;
}
catch (ThreadInterruptedException)
{
continue;
}
}
}
}
Label_0064:
return this.completed;
}
private void DispatchFutureCompleted()
{
}
public bool IsCompleted
{
get
{
return this.completed;
}
}
public bool IsSucceeded
{
get
{
return this.succeeded;
}
set
{
if (this.completed)
{
throw new InvalidOperationException("can't change the state of a completed future");
}
this.completed = true;
this.succeeded = value;
this.completedEventWaitHandler.Set();
this.DispatchFutureCompleted();
}
}
public ISession Session
{
get
{
return this.session;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -