异步(BeginInvoke,EndInvoke)
整个异步调用过程中都是围绕IAsyncResult来进行的,,BeginXXX 返回这个对象,EndXXX接收这个对象来结束当前异步对象
public interface IAsyncResult
{
WaitHandle AsyncWaitHandle { get; } //阻塞一个线程,直到一个或多个同步对象接收到信号
Boolean IsCompleted { get; } //判读当前异步是否完成
Object AsyncState { get; } //获取额外的参数值
Boolean CompletedSynchronously { get; } //几乎没有使用
}
{
WaitHandle AsyncWaitHandle { get; } //阻塞一个线程,直到一个或多个同步对象接收到信号
Boolean IsCompleted { get; } //判读当前异步是否完成
Object AsyncState { get; } //获取额外的参数值
Boolean CompletedSynchronously { get; } //几乎没有使用
}
public class AsyncResult : IAsyncResult, IMessageSink
{
//IAsyncResult 的实现
public virtual WaitHandle AsyncWaitHandle { get; }
public virtual bool IsCompleted { get; }
public virtual object AsyncState { get; }
public virtual bool CompletedSynchronously { get; }
// 其他一些重要的属性
public bool EndInvokeCalled { get; set; } //检验是否调用了EndInvoke()
public virtual object AsyncDelegate { get; } //获取原始的委托对象
}
{
//IAsyncResult 的实现
public virtual WaitHandle AsyncWaitHandle { get; }
public virtual bool IsCompleted { get; }
public virtual object AsyncState { get; }
public virtual bool CompletedSynchronously { get; }
// 其他一些重要的属性
public bool EndInvokeCalled { get; set; } //检验是否调用了EndInvoke()
public virtual object AsyncDelegate { get; } //获取原始的委托对象
}
ar.AsyncWaitHandle.WaitOne() //阻碍当前线程,直到异步调用结束.
namespace ConsoleApplication15
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("[(#{1}){0}]:Asynchronous Start", DateTime.Now.ToString(), Thread.CurrentThread.ManagedThreadId);
AsyncTest test = new AsyncTest();
ConsoleApplication15.AsyncTest.SalaryEventHandler del = test.YearlySalary;
//使用回调函数
AsyncCallback callback = new AsyncCallback(OnSalaryCallback);
IAsyncResult ar = del.BeginInvoke(100000, 15, 100000, callback, 2000);
DoAntherJob();
Console.ReadLine(); // 让黑屏等待,不会直接关闭..
}
//开始其他工作.
static void DoAntherJob()
{
Thread.Sleep(1000);//需要1秒才能完成这个工作,注1
Console.WriteLine("[(#{1}){0}]:Do Another Job", DateTime.Now.ToString(), Thread.CurrentThread.ManagedThreadId);
}
static void OnSalaryCallback(IAsyncResult asyncResult)
{
//通过AsyncState 获取额外的参数.
decimal para = (int)asyncResult.AsyncState;
//通过AsyncDelegate 获取原始的委托对象
AsyncResult obj = (AsyncResult)asyncResult;
ConsoleApplication15.AsyncTest.SalaryEventHandler del = (ConsoleApplication15.AsyncTest.SalaryEventHandler)obj.AsyncDelegate;
if (asyncResult.IsCompleted)// 判读是否已经调用完成
Console.WriteLine("[(#{1}){0}]:Asynchronous Finished.", DateTime.Now.ToString(), Thread.CurrentThread.ManagedThreadId);
decimal val = del.EndInvoke(asyncResult);
Console.WriteLine("[(#{2}){0}]:Output Result:{1}", DateTime.Now.ToString(), val + para, Thread.CurrentThread.ManagedThreadId);
}
}
public class AsyncTest
{
public delegate decimal SalaryEventHandler(decimal salary, int monthCount, decimal bonus); // 对应YearlySalary方法
public decimal YearlySalary(decimal salary, int monthCount, decimal bonus)
{
//模拟耗时/复杂的逻辑计算.
Thread.Sleep(3000);//等待3秒,注2
return salary * monthCount + bonus;
}
}
}
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("[(#{1}){0}]:Asynchronous Start", DateTime.Now.ToString(), Thread.CurrentThread.ManagedThreadId);
AsyncTest test = new AsyncTest();
ConsoleApplication15.AsyncTest.SalaryEventHandler del = test.YearlySalary;
//使用回调函数
AsyncCallback callback = new AsyncCallback(OnSalaryCallback);
IAsyncResult ar = del.BeginInvoke(100000, 15, 100000, callback, 2000);
DoAntherJob();
Console.ReadLine(); // 让黑屏等待,不会直接关闭..
}
//开始其他工作.
static void DoAntherJob()
{
Thread.Sleep(1000);//需要1秒才能完成这个工作,注1
Console.WriteLine("[(#{1}){0}]:Do Another Job", DateTime.Now.ToString(), Thread.CurrentThread.ManagedThreadId);
}
static void OnSalaryCallback(IAsyncResult asyncResult)
{
//通过AsyncState 获取额外的参数.
decimal para = (int)asyncResult.AsyncState;
//通过AsyncDelegate 获取原始的委托对象
AsyncResult obj = (AsyncResult)asyncResult;
ConsoleApplication15.AsyncTest.SalaryEventHandler del = (ConsoleApplication15.AsyncTest.SalaryEventHandler)obj.AsyncDelegate;
if (asyncResult.IsCompleted)// 判读是否已经调用完成
Console.WriteLine("[(#{1}){0}]:Asynchronous Finished.", DateTime.Now.ToString(), Thread.CurrentThread.ManagedThreadId);
decimal val = del.EndInvoke(asyncResult);
Console.WriteLine("[(#{2}){0}]:Output Result:{1}", DateTime.Now.ToString(), val + para, Thread.CurrentThread.ManagedThreadId);
}
}
public class AsyncTest
{
public delegate decimal SalaryEventHandler(decimal salary, int monthCount, decimal bonus); // 对应YearlySalary方法
public decimal YearlySalary(decimal salary, int monthCount, decimal bonus)
{
//模拟耗时/复杂的逻辑计算.
Thread.Sleep(3000);//等待3秒,注2
return salary * monthCount + bonus;
}
}
}