BeginEventHandler Delegar
Definição
Importante
Algumas informações dizem respeito a um produto pré-lançado que pode ser substancialmente modificado antes de ser lançado. A Microsoft não faz garantias, de forma expressa ou implícita, em relação à informação aqui apresentada.
Representa o método que gere eventos assíncronos, como eventos de aplicação. Este delegado é chamado no início de uma operação assíncrona.
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
Parâmetros
- sender
- Object
A origem do evento.
O delegado a chamar quando a chamada ao método assíncrono estiver concluída. Se cb for null, o delegado não é chamado.
- extraData
- Object
Quaisquer dados adicionais necessários para processar o pedido.
Devolver Valor
O IAsyncResult que representa o resultado da BeginEventHandler operação.
Exemplos
O exemplo de código seguinte utiliza o BeginEventHandler delegado para registar um handler numa página assíncrona.
Importante
Este exemplo tem uma caixa de texto que aceita a entrada do utilizador, o que constitui uma potencial ameaça à segurança. Por defeito, as páginas Web do ASP.NET validam que a entrada do utilizador não inclui elementos de script ou HTML. Para mais informações, consulte Visão Geral dos Exploits de Scripts.
<%@ 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>
Observações
Quando cria um BeginEventHandler delegado, identifica o método que irá gerir o evento. Para associar o evento ao seu gestor de eventos, adicione uma instância do delegado ao evento. O gestor de eventos é chamado sempre que o evento ocorre, a menos que remova o delegado. Para mais informações sobre os delegados handlers de eventos, consulte Gestão e Angariação de Eventos.
Métodos da Extensão
| Name | Description |
|---|---|
| GetMethodInfo(Delegate) |
Obtém um objeto que representa o método representado pelo delegado especificado. |