📄 aysnccallwrapper!1.cs
字号:
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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -