所謂的委托(Delegate)實(shí)際上就是和C/C++里面的函數(shù)指針差不多,只是增強(qiáng)了類型檢查等其它健壯性方面的內(nèi)容。異步調(diào)用的回調(diào)函數(shù)有格式要求,所謂格式要求就是參數(shù)數(shù)量及類型順序的要求,具體是什么樣的你要看文檔了。一般.NET Framework里面都是AsyncCallBack。
我們提供的服務(wù)有:成都網(wǎng)站制作、成都做網(wǎng)站、微信公眾號(hào)開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、任城ssl等。為上1000家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的任城網(wǎng)站制作公司
所謂異步編程,就是說你要求做某樣事情,但是在完成這件事之前,我能接著做下一件事,而當(dāng)這件事情完成之后,能夠有一種機(jī)制通知我完成了。相反,在完成之前一直等待,直到完成了才能進(jìn)行下一步操作,叫做同步。一般來說,我們平常寫的程序都是“同步”,或者成為“順序執(zhí)行”更加貼切,而“異步”則可以說是“亂序執(zhí)行”的。
可以看到,同步的代碼非常好寫,因?yàn)槲覀兛梢灶A(yù)測(cè)執(zhí)行的順序和情況。而異步就不是很好寫了,因?yàn)闊o(wú)法得知完成的時(shí)候我正在做什么、做到什么程度。過去寫這些代碼是比較麻煩的,實(shí)現(xiàn)的辦法就是自己建立一個(gè)處理異步事物的線程,然后在這個(gè)線程和主線程之間建立聯(lián)系。而現(xiàn)在這個(gè)過程大部分已經(jīng)被系統(tǒng)封裝起來了,你只要調(diào)用BeginXXX,系統(tǒng)就會(huì)為你自動(dòng)建立一個(gè)新的線程處理這個(gè)事情,當(dāng)前線程不阻塞,可以馬上進(jìn)行下一項(xiàng)操作,于是就實(shí)現(xiàn)了“異步”了。但是從前面我講道的你就應(yīng)該知道,開始異步操作并沒有完事,還需要能夠得知操作完成,并能夠進(jìn)行相應(yīng)的處理。于是你在調(diào)用BeginXXX的時(shí)候就需要傳遞一個(gè)回調(diào)函數(shù),在.NET里是以委托的方式傳遞的?;卣{(diào)函數(shù)的意思就是“回過頭來調(diào)用你”,或者說A調(diào)用B并且傳遞函數(shù)C的地址,于是B在指定的情況下調(diào)用A指定的函數(shù)C?,F(xiàn)在就應(yīng)該明白這個(gè)回調(diào)函數(shù)在BeginXXX中的作用就是,當(dāng)你指定的事情做完之后將會(huì)調(diào)用這個(gè)回調(diào)函數(shù)。
在這個(gè)回調(diào)函數(shù)里面,我們就可以進(jìn)行一些后續(xù)的工作,例如接著進(jìn)行性質(zhì)相同的工作,或者相應(yīng)的處理。在這里,我們也許向知道剛才那件事情執(zhí)行的情況和結(jié)果,這個(gè)時(shí)候我們就可以通過EndXXX來獲得這些東西。說到這里,結(jié)合上面說到的AsyncCallBack以及隨便哪個(gè)BeginXXX,我們可能會(huì)對(duì)下列兩個(gè)東西感到困惑:
IAsyncResult
stateObject
首先說IAsyncResult,這個(gè)是一個(gè)接口,你在回調(diào)函數(shù)中通過參數(shù)獲得的對(duì)象具體是什么類型的一般我們不需要關(guān)心,我們只需要依照這個(gè)接口的定義進(jìn)行訪問就可以了。簡(jiǎn)單點(diǎn)說,這個(gè)接口規(guī)定了順利完成異步操作所需信息的最小集合。一般來說,我們需要通過這個(gè)參數(shù)(ar)來識(shí)別異步操作。比如說,你在一瞬間發(fā)起一百個(gè)“從不同的網(wǎng)絡(luò)連接獲取數(shù)據(jù)”的異步請(qǐng)求,當(dāng)某一個(gè)請(qǐng)求被完成的時(shí)候,如何判斷是那個(gè)請(qǐng)求被完成呢?就是依靠回調(diào)函數(shù)的參數(shù)ar。實(shí)際上你一般是不需要參與判斷的,你只要將這個(gè)ar傳遞給EndXXX就可以了,EndXXX會(huì)根據(jù)這個(gè)ar自行判斷的。需要注意的是,這個(gè)ar就是你調(diào)用BeginXXX的時(shí)候的返回值,可以說是一個(gè)存根,如果你需要在完成操作前終止他,也可以通過將這個(gè)存根傳遞給EndXXX,EndXXX就會(huì)根據(jù)情況終止操作。(IAsyncResult里面的IsCompleted提供了是否已經(jīng)完成的判斷,EndXXX就是根據(jù)這個(gè)值決定是否有必要終止。當(dāng)然,你不需要關(guān)心他。)
接下來我們看看stateObject,這個(gè)東西被稱為狀態(tài)對(duì)象。于是大家就可能奇怪了:那個(gè)ar不也是狀態(tài)嗎?實(shí)際上stateObject是一個(gè)留給用戶使用的東西,BeginXXX/EndXXX根本就不使用。這個(gè)stateObject會(huì)被裝到ar的AsyncState里面,也就是說這個(gè)stateObject可以隨時(shí)通過訪問存根(BeginXXX的返回值)或者回調(diào)函數(shù)里的ar得到,你完全沒必要額外的保存到什么地方,更不需要費(fèi)神的去考慮如何和你的某個(gè)異步操作對(duì)應(yīng)起來。說了半天,這個(gè)東西有什么用呢?你想怎么用就怎么用唄,發(fā)揮一下你的想象力。比如說,你可以保存這是第幾次操作,或者在多個(gè)異步操作之間要同步的時(shí)候可以作為信號(hào)旗,再或者直接是操作這個(gè)異步操作的對(duì)象(x.BeginXXX的時(shí)候?qū)傳遞到stateObject參數(shù)上)。
第一種用法有點(diǎn)多余,第二種用法有點(diǎn)復(fù)雜,第三種我用得最多。因?yàn)槟愫芸赡茉趨f(xié)一個(gè)服務(wù)端,而服務(wù)段必須能夠響應(yīng)多個(gè)客戶端,這決定了必須用異步。同時(shí),對(duì)于多個(gè)客戶端必然有多個(gè)對(duì)象,例如網(wǎng)絡(luò)連接的時(shí)候可能是Socket。而實(shí)際上處理的方法或者協(xié)議是和具體哪個(gè)客戶端沒有關(guān)聯(lián)的,因此我們只需要一套處理程序就夠了。這個(gè)時(shí)候第三種用法就很有用處,我們完全可以把代碼寫成這樣:
Sub DataReceived(ByVal ar As IAsyncResult)
ar.AsyncState.EndReceive(ar)
ar.AsyncState.BeginReceive(... , ar.AsyncState)
End Sub
這樣就不需要額外的數(shù)據(jù)結(jié)構(gòu)去記錄有那些正在活動(dòng)的對(duì)象了。
一委托:此示例演示如何將方法與委托關(guān)聯(lián)然后通過委托調(diào)用該方法。
創(chuàng)建委托和匹配過程
創(chuàng)建一個(gè)名為 MySubDelegate 的委托。
Delegate Sub MySubDelegate(ByVal x As Integer)
聲明一個(gè)類,該類包含與該委托具有相同簽名的方法。
Class class1
Sub Sub1(ByVal x As Integer)
MsgBox("The value of x is: " CStr(x))
End Sub
End Class
定義一個(gè)方法,該方法創(chuàng)建該委托的實(shí)例并通過調(diào)用內(nèi)置的 Invoke 方法調(diào)用與該委托關(guān)聯(lián)的方法。
Protected Sub DelegateTest()
Dim c1 As New class1
' Create an instance of the delegate.
Dim msd As MySubDelegate = AddressOf c1.Sub1
' Call the method.
msd.Invoke(10)
End Sub
二、事件
下面的示例程序闡釋如何在一個(gè)類中引發(fā)一個(gè)事件,然后在另一個(gè)類中處理該事件。AlarmClock 類定義公共事件 Alarm,并提供引發(fā)該事件的方法。AlarmEventArgs 類派生自 EventArgs,并定義 Alarm 事件特定的數(shù)據(jù)。WakeMeUp 類定義處理 Alarm 事件的 AlarmRang 方法。AlarmDriver 類一起使用類,將使用 WakeMeUp 的 AlarmRang 方法設(shè)置為處理 AlarmClock 的 Alarm 事件。
該示例程序使用事件和委托和引發(fā)事件中詳細(xì)說明的概念。
示例
' EventSample.vb.
'
Option Explicit
Option Strict
Imports System
Imports System.ComponentModel
Imports Microsoft.VisualBasic
Namespace EventSample
' Class that contains the data for
' the alarm event. Derives from System.EventArgs.
'
Public Class AlarmEventArgs
Inherits EventArgs
Private _snoozePressed As Boolean
Private nrings As Integer
'Constructor.
'
Public Sub New(snoozePressed As Boolean, nrings As Integer)
Me._snoozePressed = snoozePressed
Me.nrings = nrings
End Sub
' The NumRings property returns the number of rings
' that the alarm clock has sounded when the alarm event
' is generated.
'
Public ReadOnly Property NumRings() As Integer
Get
Return nrings
End Get
End Property
' The SnoozePressed property indicates whether the snooze
' button is pressed on the alarm when the alarm event is generated.
'
Public ReadOnly Property SnoozePressed() As Boolean
Get
Return _snoozePressed
End Get
End Property
' The AlarmText property that contains the wake-up message.
'
Public ReadOnly Property AlarmText() As String
Get
If _snoozePressed Then
Return "Wake Up!!! Snooze time is over."
Else
Return "Wake Up!"
End If
End Get
End Property
End Class
' Delegate declaration.
'
Public Delegate Sub AlarmEventHandler(sender As Object, _
e As AlarmEventArgs)
' The Alarm class that raises the alarm event.
'
Public Class AlarmClock
Private _snoozePressed As Boolean = False
Private nrings As Integer = 0
Private stopFlag As Boolean = False
' The Stop property indicates whether the
' alarm should be turned off.
'
Public Property [Stop]() As Boolean
Get
Return stopFlag
End Get
Set
stopFlag = value
End Set
End Property
' The SnoozePressed property indicates whether the snooze
' button is pressed on the alarm when the alarm event is generated.
'
Public Property SnoozePressed() As Boolean
Get
Return _snoozePressed
End Get
Set
_snoozePressed = value
End Set
End Property
' The event member that is of type AlarmEventHandler.
'
Public Event Alarm As AlarmEventHandler
' The protected OnAlarm method raises the event by invoking
' the delegates. The sender is always this, the current instance
' of the class.
'
Protected Overridable Sub OnAlarm(e As AlarmEventArgs)
RaiseEvent Alarm(Me, e)
End Sub
' This alarm clock does not have
' a user interface.
' To simulate the alarm mechanism it has a loop
' that raises the alarm event at every iteration
' with a time delay of 300 milliseconds,
' if snooze is not pressed. If snooze is pressed,
' the time delay is 1000 milliseconds.
'
Public Sub Start()
Do
nrings += 1
If stopFlag Then
Exit Do
Else
If _snoozePressed Then
System.Threading.Thread.Sleep(1000)
If (True) Then
Dim e As New AlarmEventArgs(_snoozePressed, nrings)
OnAlarm(e)
End If
Else
System.Threading.Thread.Sleep(300)
Dim e As New AlarmEventArgs(_snoozePressed, nrings)
OnAlarm(e)
End If
End If
Loop
End Sub
End Class
' The WakeMeUp class has a method AlarmRang that handles the
' alarm event.
'
Public Class WakeMeUp
Public Sub AlarmRang(sender As Object, e As AlarmEventArgs)
Console.WriteLine((e.AlarmText + ControlChars.Cr))
If Not e.SnoozePressed Then
If e.NumRings Mod 10 = 0 Then
Console.WriteLine(" Let alarm ring? Enter Y")
Console.WriteLine(" Press Snooze? Enter N")
Console.WriteLine(" Stop Alarm? Enter Q")
Dim input As String = Console.ReadLine()
If input.Equals("Y") Or input.Equals("y") Then
Return
Else
If input.Equals("N") Or input.Equals("n") Then
CType(sender, AlarmClock).SnoozePressed = True
Return
Else
CType(sender, AlarmClock).Stop = True
Return
End If
End If
End If
Else
Console.WriteLine(" Let alarm ring? Enter Y")
Console.WriteLine(" Stop Alarm? Enter Q")
Dim input As String = Console.ReadLine()
If input.Equals("Y") Or input.Equals("y") Then
Return
Else
CType(sender, AlarmClock).Stop = True
Return
End If
End If
End Sub
End Class
' The driver class that hooks up the event handling method of
' WakeMeUp to the alarm event of an Alarm object using a delegate.
' In a forms-based application, the driver class is the
' form.
'
Public Class AlarmDriver
Public Shared Sub Main()
' Instantiates the event receiver.
Dim w As New WakeMeUp()
' Instantiates the event source.
Dim clock As New AlarmClock()
' Wires the AlarmRang method to the Alarm event.
AddHandler clock.Alarm, AddressOf w.AlarmRang
clock.Start()
End Sub
End Class
End Namespace
Public Class SP
Public P As New ToolStrip
' Delegate Sub B_CLIKE(tb As Object, e As EventArgs)
' Public Sub ZR(Name As String, BClick As B_CLIKE)
Public Sub ZR(Name As String, BClick As EventHandler)
Dim b As New ToolStripButton
b.Text = Name
b.DisplayStyle = ToolStripItemDisplayStyle.Text
AddHandler b.Click, BClick
' AddHandler b.Click, AddressOf BClick
P.Items.Add(b)
End Sub
End Class