對於C#委托我們談的比較多
C#委托的Invoke方法用來進行同步調用
同步調用的例子
using System;
using System
public delegate int AddHandler(int a
public class Foo
{
static void Main()
{
Console
AddHandler handler = new AddHandler(Add);
int result = handler
Console
Console
}
static int Add(int a
{ Console
Thread
Console
return a+b;
}
}
運行結果
**********SyncInvokeTest************** Computing
異步調用
using System;
using System
public delegate int AddHandler(int a
public class Foo
{
static void Main()
{
Console
AddHandler handler = new AddHandler(Add);
IAsyncResult result = handler
Console
Console
Console
}
static int Add(int a
{
Console
Thread
Console
return a+b;
}
}
運行結果
**********AsyncInvokeTest************** Do other work
可以看到
但是問題依然存在
解決的辦法是用回調函數
回調異步
public class Foo
{
static void Main() {
Console
AddHandler handler = new AddHandler(Add);
IAsyncResult result = handler
Console
}
static int Add(int a
{
Console
Thread
Console
return a+b;
}
static void AddComplete(IAsyncResult result)
{
AddHandler handler = (AddHandler)((AsyncResult)result)
Console
Console
}
}
From:http://tw.wingwit.com/Article/program/ASP/201311/21819.html