【www.bbyears.com--php函数】
A、 用回调函数(本例中为CallBackMethod),异步结束后,自动调用此回调函数。
B、 而不在主线程中手工等待异步结束,如上两例中在主线程中调用EndInvoke。此种方法,是在回调函数中调用EndInvoke的。
异步回调的大概流程是这样的:首先启动异步,启动参数加上异步结束时执行的方法,然后这个异步线程就不用管了,最后当这个异步线程自己完成工作了,就自动执行启动参数里的那个方法,这样确实很省心,可是代码写起来,就很复杂了。
一个最简单的异步调用
private delegate int AsyncFuncDelegate(int m);
private int AsyncFunc(int m)
{
for (int i = 0; i < 999999999; i++)
{
m += i;
}
return m;
}
private void button1_Click(object sender, EventArgs e)
{
AsyncFuncDelegate afd = AsyncFunc;
afd.BeginInvoke(1, null, null);
}
如上,针对要异步调用的方法建一个委托,再利用 BeginInvoke 来异步调用,BeginInvoke 的参数是在委托对应的参数后面再加两个参数,这两个参数我们后面会讲,这里先为 null
方法二
代码如下 //首先准备好,要进行异步的方法(能异步的,最好不多线程)
privatestringMethodName(intNum,outintNum2)
{
Num2=Num;
return"HelloWorld";
}
//程序终点
//异步完成时,执行的方法(回调方法),此方法只能有IAsyncResult一个参数,但是该参数几乎万能,可以传递object
privatevoidCallBackMethod(IAsyncResultar)
{
//从异步状态ar.AsyncState中,获取委托对象
DelegateNamedn=(DelegateName)ar.AsyncState;
//输出参数
inti;
//一定要EndInvoke,否则你的下场很惨
stringr=dn.EndInvoke(outi,ar);
MessageBox.Show("异步完成喽!i的值是"i.ToString()",r的值是"r);
}
//定义与方法同签名的委托
privatedelegatestringDelegateName(intNum,outintNum2);
//程序入口
privatevoidRun()
{
//实例化委托并初赋值
DelegateNamedn=newDelegateName(MethodName);
//输出参数
inti;
//实例化回调方法
//把AsyncCallback看成Delegate你就懂了,实际上AsyncCallback是一种特殊的Delegate,就像Event似的
AsyncCallbackacb=newAsyncCallback(CallBackMethod);
//异步开始
//如果参数acb换成null则表示没有回调方法
//最后一个参数dn的地方,可以换成任意对象,该对象可以被回调方法从参数中获取出来,写成null也可以。参数dn相当于该线程的ID,如果有多个异步线程,可以都是null,但是绝对不能一样,不能是同一个object,否则异常
IAsyncResultiar=dn.BeginInvoke(1,outi,acb,dn);
//去做别的事
//…………
}
//最后的结果应该是:i=1,r="HelloWorld"
//另外,如果可以,定义委托的时候可以选择不用过多的修饰:
///
///定义委托
///
///
publicdelegateboolAsyncdelegate();
///
///Callbackmethodmusthavethesamesignatureasthe
///AsyncCallbackdelegate
///
///
privatevoidCallbackMethod(IAsyncResultar)
{
//Retrievethedelegate.
Asyncdelegatedlgt=(Asyncdelegate)ar.AsyncState;
//CallEndInvoketoretrievetheresults.
dlgt.EndInvoke(ar);
}
//其他方法中调用:
//异步执行
//指定委托方法
Asyncdelegateisgt=newAsyncdelegate(icpInfo.Insert);
IAsyncResultar=isgt.BeginInvoke(newAsyncCallback(CallbackMethod),isgt);
方法三
代码如下using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace CW
{
public partial class AsyncDemo : Form
{
public AsyncDemo()
{
InitializeComponent();
}
private void Delgate_Load(object sender, EventArgs e)
{
}
///
/// 实现委托的方法
///
///
///
///
string LongRunningMethod(int iCallTime, out int iExecThread)
{
Thread.Sleep(iCallTime);
iExecThread = AppDomain.GetCurrentThreadId();
return "MyCallTime was " + iCallTime.ToString();
}
delegate string MethodDelegate(int iCallTime, out int iExecThread);
#region 示例 1: 同步调用方法#region 示例 1: 同步调用方法
/**/
/*
* 同步调用方法
* */
/**/
///
/// 示例 1: 同步调用方法
///
public void DemoSyncCall()
{
string s;
int iExecThread;
// Create an instance of a delegate that wraps LongRunningMethod.
MethodDelegate dlgt = new MethodDelegate(this.LongRunningMethod);
// Call LongRunningMethod using the delegate.
s = dlgt(3000, out iExecThread);
MessageBox.Show(string.Format ("The delegate call returned the string: {0}, and the thread ID {1}", s, iExecThread.ToString() ) );
}
#endregion
#region 示例 2: 通过 EndInvoke() 调用模式异步调用方法
/**/
/*
* 使用调用模式是要调用 BeginInvoke , 做某些处理主线程, 并调用 EndInvoke() 。
* 注意不 EndInvoke() 不返回直到异步调用已完成。
* 此调用模式是有用当要有调用线程正在执行异步调用, 同时工作。
* 有同时发生工作可改善许多应用程序的性能。
* 常见任务以异步运行以此方式是文件或网络操作。
* */
/**/
///
/// 示例 2: 通过 EndInvoke() 调用模式异步调用方法
///
public void DemoEndInvoke()
{
MethodDelegate dlgt = new MethodDelegate(this.LongRunningMethod);
string s;
int iExecThread;
// Initiate the asynchronous call.
IAsyncResult ar = dlgt.BeginInvoke(5000, out iExecThread, null, null);
// Do some useful work here. This would be work you want to have
// run at the same time as the asynchronous call.
// Retrieve the results of the asynchronous call.
s = dlgt.EndInvoke(out iExecThread, ar);
MessageBox.Show(string.Format ("The delegate call returned the string: {0}, and the number {1}", s, iExecThread.ToString() ) );
}
#endregion
#region 示例 3: 异步调用方法并使用 A WaitHandle 来等待调用完成
/**/
/*
* 由 BeginInvoke() 返回 IAsyncResult 具有一个 AsyncWaitHandle 属性。
* 该属性返回 WaitHandle 异步调用完成后, 通知是。 等待 WaitHandle 是常见线程同步技术。
* 通过是 WaitHandle WaitOne() 方法调用线程等待 WaitHandle 上。
* 直到是通知 WaitHandle WaitOne() 块。 当 WaitOne() 返回, 您在调用 EndInvoke() 之前进行一些额外工作。
* 对于执行文件或网络操作, 否则会阻塞调用主线程存为, 以前示例中此技术很有用。
* */
/**/
///
/// 示例 3: 异步调用方法并使用 A WaitHandle 来等待调用完成
///
public void DemoWaitHandle()
{
string s;
int iExecThread;
MethodDelegate dlgt = new MethodDelegate(this.LongRunningMethod);
// Initiate the asynchronous call.
IAsyncResult ar = dlgt.BeginInvoke(3000, out iExecThread, null, null);
// Do some useful work here. This would be work you want to have
// run at the same time as the asynchronous call.
// Wait for the WaitHandle to become signaled.
ar.AsyncWaitHandle.WaitOne();
// Get the results of the asynchronous call.
s = dlgt.EndInvoke(out iExecThread, ar);
MessageBox.Show(string.Format ("The delegate call returned the string: {0}, and the number {1}", s, iExecThread.ToString() ) );
}
#endregion
#region 示例 4: 异步调用方法通过轮询调用模式
/**/
/*
* 由 BeginInvoke() 返回 IAsyncResult 对象有个 IsCompleted 属性异步调用完成后返回 True 。
* 然后可调用 EndInvoke() 。 如果您应用程序不断工作对不做要长期函数调用已被此调用模式很有用。
* MicrosoftWindows 应用程序是这样的示例。
* 主线程的 Windows 应用程序可以继续以执行异步调用时处理用户输入。
* 它可定期检查 IsCompleted 到调用是否完成。 它调用 EndInvoke 当 IsCompleted 返回 True 。
* 直到它知道操作已完成因为 EndInvoke() 阻止直到异步操作为完整, 应用程序不调用它。
* */
/**/
///
/// 示例 4: 异步调用方法通过轮询调用模式
///
public void DemoPolling()
{
MethodDelegate dlgt = new MethodDelegate(this.LongRunningMethod);
string s;
int iExecThread;
// Initiate the asynchronous call.
IAsyncResult ar = dlgt.BeginInvoke(3000, out iExecThread, null, null);
// Poll IAsyncResult.IsCompleted
while (ar.IsCompleted == false)
{
Thread.Sleep(10); // pretend to so some useful work
}
s = dlgt.EndInvoke(out iExecThread, ar);
MessageBox.Show(string.Format ("The delegate call returned the string: {0}, and the number {1}", s, iExecThread.ToString() ) );
}
#endregion
#region 示例 5: 异步方法完成后执行回调
/**/
/*
* 本节, 中示例提供对 BeginInvoke() 函数, 异步调用完成后系统执行回调委托。
* 回调调用 EndInvoke() 并处理异步调用的结果。
* 如果启动异步调用线程不需要处理结果是调用此调用模式很有用。
* 异步调用完成后系统调用线程以外启动线程上调。
* 若使用此调用模式, 作为第二到最后 - BeginInvoke() 函数的参数必须传递 AsyncCallback 类型的委托。
* BeginInvoke() 还有最后参数键入 对象 到您可以将任何对象。 当它调用该对象可用于您回调函数。
* 为此参数一个重要用途是以传递用于初始化调用该委托。
* 回调函数然后使用与该委托 EndInvoke() 函数来完成调用。 此调用模式是所示。
* */
/**/
///
/// 示例 5: 异步方法完成后执行回调
///
public void DemoCallback()
{
MethodDelegate dlgt = new MethodDelegate(this.LongRunningMethod);
int iExecThread;
// Create the callback delegate.
AsyncCallback cb = new AsyncCallback(MyAsyncCallback);
// Initiate the Asynchronous call passing in the callback delegate
// and the delegate object used to initiate the call.
IAsyncResult ar = dlgt.BeginInvoke(5000, out iExecThread, cb, dlgt);
}
public void MyAsyncCallback(IAsyncResult ar)
{
string s;
int iExecThread;
// Because you passed your original delegate in the asyncState parameter
// of the Begin call, you can get it back here to complete the call.
MethodDelegate dlgt = (MethodDelegate)ar.AsyncState;
// Complete the call.
s = dlgt.EndInvoke(out iExecThread, ar);
MessageBox.Show(String.Format("The delegate call returned the string: {0}, and the number {1}", s, iExecThread.ToString()));
//Console.WriteLine(string.Format ("The delegate call returned the string: "{0}", and the number {1}", s, iExecThread.ToString() ) );
}
#endregion
private void button1_Click(object sender, EventArgs e)
{
//DemoSyncCall() ;
//DemoEndInvoke();
//DemoWaitHandle();
//DemoPolling();
DemoCallback();
}
}
}
异步出来的新线程,必须回收,不回收是浪费资源的可耻行为,.NET也是不允许的,所以你别想钻空子,俗话说,请神容易送神难,就是这个道理。下面你可以很容易想到,回收分为2种情况:主动回收和被动回收(当然,这是我自己的理解,微软可不是这么说的),主动回收就是,你去监视那个线程,并且等待,当异步方法完成了,就把异步线程回收,焦点回归主线程,实际上就是上篇文章《C#异步初步》的那种情况,BeginInvoke之后又EndInvoke,如果在EndInvoke的时候,该异步线程没有完成操作,那么整个程序,包括主线程,又在阻塞了,又会出现界面“死”的情况。要想解决这个问题,就使用“被动回收”方式,其中一个重要的办法就是“异步回调”。