BeginEventHandler 代理人
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
表示處理非同步事件 (例如應用程式事件) 的方法。 這個委派 (Delegate) 是在非同步操作的開始呼叫。
public delegate IAsyncResult ^ BeginEventHandler(System::Object ^ sender, EventArgs ^ e, AsyncCallback ^ cb, System::Object ^ extraData);
public delegate IAsyncResult BeginEventHandler(object sender, EventArgs e, AsyncCallback cb, object extraData);
type BeginEventHandler = delegate of obj * EventArgs * AsyncCallback * obj -> IAsyncResult
Public Delegate Function BeginEventHandler(sender As Object, e As EventArgs, cb As AsyncCallback, extraData As Object) As IAsyncResult
參數
- sender
- Object
事件的來源。
非同步方法呼叫完成後,要呼叫的委派。 如果 cb
為 null
,則不呼叫委派。
- extraData
- Object
處理要求時所需的任何額外資料。
傳回值
IAsyncResult,表示 BeginEventHandler 作業的結果。
範例
下列程式代碼範例會 BeginEventHandler 使用 委派來註冊異步頁面的處理程式。
重要
這個範例有一個可接受使用者輸入的文字方塊,這可能會造成安全性威脅。 根據預設,ASP.NET Web 網頁會驗證使用者輸入未包含指令碼或 HTML 項目。 如需詳細資訊,請參閱 Script Exploits Overview (指令碼攻擊概觀)。
<%@ page language="C#" Async="true"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
System.Net.WebRequest myRequest;
void Page_Load(object sender, EventArgs e)
{
Label1.Text = "Page_Load: thread #" + System.Threading.Thread.CurrentThread.GetHashCode();
BeginEventHandler bh = new BeginEventHandler(this.BeginGetAsyncData);
EndEventHandler eh = new EndEventHandler(this.EndGetAsyncData);
AddOnPreRenderCompleteAsync(bh, eh);
// Initialize the WebRequest.
string address = "http://localhost/";
myRequest = System.Net.WebRequest.Create(address);
}
IAsyncResult BeginGetAsyncData(Object src, EventArgs args, AsyncCallback cb, Object state)
{
Label2.Text = "BeginGetAsyncData: thread #" + System.Threading.Thread.CurrentThread.GetHashCode();
return myRequest.BeginGetResponse(cb, state);
}
void EndGetAsyncData(IAsyncResult ar)
{
Label3.Text = "EndGetAsyncData: thread #" + System.Threading.Thread.CurrentThread.GetHashCode();
System.Net.WebResponse myResponse = myRequest.EndGetResponse(ar);
result.Text = new System.IO.StreamReader(myResponse.GetResponseStream()).ReadToEnd();
myResponse.Close();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>
Page.AddOnPreRenderCompleteAsync Example</title>
</head>
<body>
<form id="form1" runat="server">
<asp:label id="Label1" runat="server">
Label 1</asp:label><br />
<asp:label id="Label2" runat="server">
Label 2</asp:label><br />
<asp:label id="Label3" runat="server">
Label 3</asp:label><br />
<asp:textbox id="result" runat="server" textMode="multiLine" ReadOnly="true" columns="80" rows="25" />
</form>
</body>
</html>
<%@ page language="VB" Async="true"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Dim myRequest As System.Net.WebRequest
Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
Label1.Text = "Page_Load: Thread #" & System.Threading.Thread.CurrentThread.GetHashCode()
Dim bh As New BeginEventHandler(AddressOf Me.BeginGetAsyncData)
Dim eh As New EndEventHandler(AddressOf Me.EndGetAsyncData)
Me.AddOnPreRenderCompleteAsync(bh, eh)
' Initialize the WebRequest object.
Dim address As String
address = "http://localhost/"
myRequest = System.Net.WebRequest.Create(address)
End Sub
Function BeginGetAsyncData(ByVal src As Object, ByVal args As EventArgs, ByVal cb As AsyncCallback, ByVal state As Object) As IAsyncResult
Label2.Text = "BeginGetAsyncData: Thread #" & System.Threading.Thread.CurrentThread.GetHashCode()
Return Me.myRequest.BeginGetResponse(cb, state)
End Function
Sub EndGetAsyncData(ByVal ar As IAsyncResult)
Label3.Text = "EndGetAsyncData: Thread #" & System.Threading.Thread.CurrentThread.GetHashCode()
Dim myResponse As System.Net.WebResponse
myResponse = Me.myRequest.EndGetResponse(ar)
Dim reader As New System.IO.StreamReader(myResponse.GetResponseStream())
result.Text = reader.ReadToEnd()
myResponse.Close()
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>
Page.AddOnPreRenderCompleteAsync Example</title>
</head>
<body>
<form id="form1" runat="server">
<asp:label id="Label1" runat="server">
Label 1</asp:label><br />
<asp:label id="Label2" runat="server">
Label 2</asp:label><br />
<asp:label id="Label3" runat="server">
Label 3</asp:label><br />
<asp:textbox id="result" runat="server" textMode="multiLine" ReadOnly="true" columns="80" rows="25" />
</form>
</body>
</html>
備註
建立 BeginEventHandler 委派時,必須識別處理事件的方法。 若要使事件與您的事件處理常式產生關聯,請將委派的執行個體 (Instance) 加入至事件。 除非您移除委派,否則每當事件發生時就會呼叫事件處理常式。 如需事件處理程式委派的詳細資訊,請參閱 處理和引發事件。
擴充方法
GetMethodInfo(Delegate) |
取得表示特定委派所代表之方法的物件。 |