Here ist the complete code:
Business Logic:
using System;
using System.Collections.Generic;
using System.Web;
public class CustomEventArgs : EventArgs
{
public CustomEventArgs(string s)
{
message = s;
}
private string message;
public string Message
{
get { return message; }
set { message = value; }
}
}
/// <summary>
/// Summary description for BusinessLogic
/// </summary>
public class BusinessLogic
{
public event EventHandler<CustomEventArgs> StatusEvent;
public BusinessLogic()
{
}
public void DoSomething()
{
RaiseStatusEvent("Start");
System.Threading.Thread.Sleep(60000);
RaiseStatusEvent("Status 1");
System.Threading.Thread.Sleep(60000);
RaiseStatusEvent("Status 2");
System.Threading.Thread.Sleep(60000);
RaiseStatusEvent("Status 3");
RaiseStatusEvent("Ende");
}
private void RaiseStatusEvent(string Info)
{
if (this.StatusEvent != null)
StatusEvent(Info,new CustomEventArgs(Info));
}
}
Default.aspx.c
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Runtime.Remoting.Messaging;
using System.Threading;
public partial class _Default : System.Web.UI.Page,ICallbackEventHandler
{
static System.Collections.Generic.List<string> Messages;
protected void Page_Load(object sender, EventArgs e)
{
Messages = new System.Collections.Generic.List<string>();
}
private void LongRunningMethod(string name)
{
BusinessLogic BL = new BusinessLogic();
BL.StatusEvent += StatusEventHandler;
BL.DoSomething();
//Thread.Sleep(60000 * 5);//This will run for 5 mins
}
private void StatusEventHandler(object sender, CustomEventArgs e)
{
Messages.Add(e.Message);
}
private delegate void LongRun(string name);
private delegate void Status(string Info);
private IAsyncResult DoSomthing()
{
LongRun Long = new LongRun(LongRunningMethod);// Delegate will call LongRunningMethod
IAsyncResult ar = Long.BeginInvoke("CHECK",new AsyncCallback(CallBackMethod),null);
//”CHECK” will go as function parameter to LongRunningMethod
return ar; //Once LongRunningMethod get over CallBackMethod method will be invoked.
}
private void CallBackMethod(IAsyncResult ar)
{
AsyncResult Result = (AsyncResult)ar;
LongRun Long = (LongRun)Result.AsyncDelegate;
Long.EndInvoke(Result);
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write("Long running process is started");
Session["result"] = null;
IAsyncResult res = DoSomthing();
Session["result"] = res;
//You can redirect to some other page, prcoess will continue..
//since we are using session variable it will be available on another page also
//and you just need to check for process completion as we did in GetCallbackResult().
//On that page also we will have to use ICallback for priodic check.
}
public string GetCallbackResult()
{
if (Session["result"] != null)
{
IAsyncResult res = (IAsyncResult)Session["result"];
if (res.IsCompleted)//Checking whether process is over or not
{
return "SUCCESS";
}
else
{
System.Text.StringBuilder MessageText = new System.Text.StringBuilder();
foreach (string item in Messages)
{
MessageText.AppendLine("Message:" + item);
}
return MessageText.ToString();
}
}
return "FAIL";
}
public void RaiseCallbackEvent(string args)
{
//IF any operation do it here.
}
}
And finaly the html code of default.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" EnableSessionState="True"
Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml...">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Long Running Process</title>
</head>
<body onload="CallServer();">
<form id="form1" runat="server">
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<div id="Wait">
Click button to start long running process.</div>
<div id="Message">
Message goes here</div>
<script language="javascript">
function CallServer()
{
window.document.getElementById("Wait").innerText = window.document.getElementById("Wait").innerText + ".";
<%= ClientScript.GetCallbackEventReference(this,"", "ShowResult", null) %>;
}
function ShowResult(eventArgument,context)
{
if(eventArgument == "SUCCESS")
{
Done');
}
else {
window.document.getElementById("Message").innerText = eventArgument;
setTimeout("CallServer()",20000);//Priodic call to server.
}
}
</script>
</form>
</body>
</html>