aysnccallwrapper!1.cs
来自「破解的飞信源代码」· CS 代码 · 共 89 行
CS
89 行
namespace NCindy.Util
{
using System;
using System.Runtime.CompilerServices;
using System.Threading;
public class AysncCallWrapper<T> : IDisposable
{
private ManualResetEvent doneEvent;
private Exception exception;
private AysncCallFunction<T, T> function;
private bool isDone;
private T value;
public AysncCallWrapper(AysncCallFunction<T, T> functionToRun)
{
this.function = functionToRun;
ThreadPool.QueueUserWorkItem(new WaitCallback(this.Worker));
}
public void Dispose()
{
if (this.doneEvent != null)
{
this.doneEvent.Close();
this.doneEvent = null;
}
}
private void Worker(object obj)
{
try
{
this.value = this.function();
}
catch (Exception exception)
{
this.exception = exception;
}
finally
{
this.isDone = true;
Thread.MemoryBarrier();
if (this.doneEvent != null)
{
this.doneEvent.Set();
}
}
}
public bool IsDone
{
get
{
return this.isDone;
}
}
public T Value
{
get
{
if (!this.isDone)
{
this.doneEvent = new ManualResetEvent(false);
Thread.MemoryBarrier();
if (this.isDone)
{
this.doneEvent.Close();
this.doneEvent = null;
}
else
{
this.doneEvent.WaitOne();
}
}
if (this.exception != null)
{
throw this.exception;
}
return this.value;
}
}
public delegate T AysncCallFunction<T>();
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?