一、同步委托
我們平時所用的委托以同步居多,我們編寫一個方法和相關委托進行演示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
publicdelegatevoid DoSomethingDelegate(
string
name);
//同步委托
public
static
void
Start1()
{
Console.WriteLine(
"this is primary thread"
);
Console.WriteLine(
"main thread:{0},{1},{2}"
, Thread.CurrentThread.CurrentCulture, Thread.CurrentThread.Name, Thread.CurrentThread.ManagedThreadId);
//DoSomethingDelegate del = new DoSomethingDelegate(Method1);
//注意這里,簡單起見還可以把一個方法名直接賦給一個委托類型
DoSomethingDelegate del = Method1;
del(
"this is delegate method"
);
}
//委托所關聯的方法
public
static
void
Method1(
string
name)
{
Console.WriteLine(
"sub thread: {0},{1},{2}"
, Thread.CurrentThread.CurrentCulture, Thread.CurrentThread.Name, Thread.CurrentThread.ManagedThreadId);
Console.WriteLine(name);
Thread.Sleep(TimeSpan.FromSeconds(3));
Console.WriteLine(
"sub thread other things..."
);
}
|
我們分析下這個Start1()方法,首先顯示了主線程相關的信息,然后定義了一個委托類型del,利用del("this is delegate method")執行Method1(string name)方法,由於是同步委托,所以主線程在執行到Thread.Sleep(TimeSpan.FromSeconds(3));處會暫時掛起,3秒后才繼續執行,然后才返回到Start1()方法中繼續執行。
我們運行Start1()方法后看看執行順序
可以看到,運行結果是按主線程的執行順序依次往下執行。
二、異步委托
1
2
3
4
5
6
7
8
9
|
//異步委托
public
static
void
Start2()
{
Console.WriteLine(
"main thread:{0},{1},{2}"
, Thread.CurrentThread.CurrentCulture, Thread.CurrentThread.Name, Thread.CurrentThread.ManagedThreadId);
//DoSomethingDelegate del = new DoSomethingDelegate(Method1);
DoSomethingDelegate del = Method1;
del.BeginInvoke(
"this is delegate method"
,
null
,
null
);
Console.WriteLine(
"main thread other things..."
);
}
|
此次我們利用委托的BeginInvoke方法進行方法調用,BeginInvoke的方法簽名如下:
1
|
IAsyncResult DoSomethingDelegate.BeginInvoke(
string
name,AsyncCallBack callback,
object
@
object
)
|
那么利用BeginInvoke進行方法調用的結果如何呢?如結果顯示,BeginInvoke調用的方法有一個子線程去調用,主線程沒有被執行到,Thread.Sleep(TimeSpan.FromSeconds(3));這個方法,也就沒有被掛起線程。

三、異步委托詳解
剛才我們通過del.BeginInvoke("this is delegate method", null, null);這樣就做到了異步調用,我們在編寫代碼中還有這樣一種需求,如果你要進行異步調用,子線程執行的結果怎么返回給主線程呢?del.EndInvoke上場了!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
//異步委托得到返回值,實際上為了得到返回值,阻礙了主線程
public
static
void
Start3()
{
Console.WriteLine(
"main thread:{0},{1},{2}"
, Thread.CurrentThread.CurrentCulture, Thread.CurrentThread.Name, Thread.CurrentThread.ManagedThreadId);
//DoSomethingDelegate del = new DoSomethingDelegate(Method1);
DoSomethingDelegate2 del = Method2;
IAsyncResult result=del.BeginInvoke(
"this is delegate method"
,
null
,
null
);
string
s = del.EndInvoke(result);
Console.WriteLine(
"得到返回值:"
+ s);
Console.WriteLine(
"main thread other things..."
);
}
//異步委托所調用的方法,注意此方法有返回值
public
static
string
Method2(
string
name)
{
Console.WriteLine(
"sub thread:{0},{1},{2}"
, Thread.CurrentThread.CurrentCulture, Thread.CurrentThread.Name, Thread.CurrentThread.ManagedThreadId);
Console.WriteLine(name);
Thread.Sleep(TimeSpan.FromSeconds(3));
Console.WriteLine(
"sub thread other things..."
);
return
"返回委托值"
;
}
|
從實例代碼中我們可以看到,我們為了得到異步方法的返回值寫了這么兩行代碼:
1
2
|
IAsyncResult result=del.BeginInvoke(
"this is delegate method"
,
null
,
null
);
string
s = del.EndInvoke(result);
|

我們查看執行結果:由運行結果可以看到,屏幕輸出了返回值,但是Method2(string name)方法並沒有被異步執行到!原因在於string s = del.EndInvoke(result);這句阻礙了主線程的繼續執行,等子線程返回值后賦給s后,主線程才繼續執行。這樣寫的后果就是:為了得到返回值,阻礙了主線程
我們剛才執行異步委托都是通過下面的代碼來完成的
1
|
IAsyncResult result=del.BeginInvoke(
"this is delegate method"
,
null
,
null
);
|
我們將BeginInvoke方法的第二個和第三個參數都設置為了null,我們現在來看看這兩個參數的作用!第二個參數AsyncCallBack callback,這個參數實際上是一個回調委托,我們看此委托的定義:
1
|
public
delegate
void
AsyncCallback(IAsyncResult ar);
|
什么是回調方法?就是說委托所調用的方法執行完畢后自動執行的方法,即上面的Method2(string name)方法被異步執行結束后所調用的方法。於是我們在定義一個跟AsyncCallback委托匹配的方法:
1
2
3
4
5
6
|
public
static
void
CallBack(IAsyncResult result)
{
DoSomethingDelegate2 del = result.AsyncState
as
DoSomethingDelegate2;
string
s = del.EndInvoke(result);
Console.WriteLine(
"得到返回值:"
+ s);
}
|
1
2
3
4
5
6
7
8
|
public
static
void
Start4()
{
Console.WriteLine(
"main thread:{0},{1},{2}"
, Thread.CurrentThread.CurrentCulture, Thread.CurrentThread.Name, Thread.CurrentThread.ManagedThreadId);
DoSomethingDelegate2 del = Method2;
AsyncCallback callBack = CallBack;
del.BeginInvoke(
"this is delegate method"
, callBack, del);
Console.WriteLine(
"main thread other things..."
);
}
|
1
2
3
4
5
6
7
8
|
public
static
string
Method2(
string
name)
{
Console.WriteLine(
"sub thread:{0},{1},{2}"
, Thread.CurrentThread.CurrentCulture, Thread.CurrentThread.Name, Thread.CurrentThread.ManagedThreadId);
Console.WriteLine(name);
Thread.Sleep(TimeSpan.FromSeconds(3));
Console.WriteLine(
"sub thread other things..."
);
return
"返回委托值"
;
}
|
從上面的代碼可以看出,在CallBack方法中我們得到了Method2(string name)方法的返回值。並且整個過程是異步執行的!請看運行結果:
為了得到異步方法的返回值還可以這么做:
1
2
3
4
5
6
7
8
|
public
static
void
Start4()
{
Console.WriteLine(
"main thread:{0},{1},{2}"
, Thread.CurrentThread.CurrentCulture, Thread.CurrentThread.Name, Thread.CurrentThread.ManagedThreadId);
DoSomethingDelegate2 del = Method2;
//另一種實現方法
del.BeginInvoke(
"this is delegate method"
, CallBack2,
null
);
Console.WriteLine(
"main thread other things..."
);
}
|
1
2
3
4
5
6
7
|
public
static
void
CallBack2(IAsyncResult result)
{
AsyncResult ar = result
as
AsyncResult;
DoSomethingDelegate2 del = ar.AsyncDelegate
as
DoSomethingDelegate2;
string
s=del.EndInvoke(ar);
Console.WriteLine(
"得到返回值:"
+ s);
}
|
這段代碼的運行效果跟上面是一樣的,只不過寫法不同而已!