Misc -- clues --to Async.

Objectives :
  • AsyncCallback, IAsyncResult
  • GetCustomAttributes
  • [System.AttributeUsage(System.AttributeTargets.Class |
    System.AttributeTargets.Struct,
    AllowMultiple = true)] // multiuse attribute

Step: 1 Create a website ( http://manas6/aspnet.35/mm.Async8/  )

Step: 2 Code

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>mm.Async8: AsyncCallBack </title>
</head>
<body>
<form id="form1" runat="server">
<div> In this example One method is called with AsyncCallBack using a <br />delegate object in an another method. The caller method is initiated with page_load <br />
<asp:Label ID="L1" runat="server" Text="--------"></asp:Label>
</div>
</form>
</body>
</html>
 

Default.aspx.cs

using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Collections.Specialized;
using System.Collections;
// Async
using System.Runtime.Remoting.Messaging;

//
delegate string MyDelegate(string str);
public partial class _Default : System.Web.UI.Page
{

protected void Page_Load(object sender, EventArgs e)
{
MyDelegate del = new MyDelegate(Ping);
AsyncCallback callback = new AsyncCallback(Pong);
IAsyncResult ar = del.BeginInvoke("Hello", callback, null);
while (!ar.IsCompleted)
{
L1.Text += "<br/>" + String.Format("waiting on a thread...");
System.Threading.Thread.Sleep(1000);
}

}
protected string Ping(string str)
{
// send string to a caller when needed
L1.Text += "<br/> Ping Area : Ping(string str) = " + str;
return str;

}
protected void Pong(IAsyncResult ar)
{
L1.Text += "<br/> Pong talking indrectly via AsyncDelegate ";// +ar.AsyncState.ToString();

MyDelegate X = (MyDelegate)((AsyncResult)ar).AsyncDelegate;
L1.Text += "<br/> Pong talking EndInvoke ";

X.EndInvoke(ar);

}

}
 

Step: 3