Question

I saw an example of forever iframe implementation ( comet simulation ) , so I decided to test it but with the addition of asynchronous approach , so that there will be no blocking.

Pretty simple :

I have a page (index.html) with hidden iframe which has SRC of AdminPush.aspx:

/*1*/           protected void Page_Load(object sender, EventArgs e)
/*2*/           {
/*3*/               UpdateMessage();
/*4*/           }
/*5*/   
/*6*/   
/*7*/           protected void UpdateMessage()
/*8*/           {
/*9*/               HttpContext.Current.Response.ContentType = "text/html";
/*10*/               Response.Write("<script  >parent.UpdateMessage(DateTime.Now.Second)</script>");
/*11*/               Response.Flush();
/*12*/   
/*13*/               //async part goes here  !!
/*14*/               this.RegisterAsyncTask(new PageAsyncTask(async cancellationToken =>
/*15*/                                       {
/*16*/                                            await Task.Delay(2000, cancellationToken);
/*17*/                                            UpdateMessage();
/*18*/                                       }));
/*19*/           }

On the AdminPush.aspx Page I added :

Async="true"

On the html page (index.html) I added :

function UpdateMessage(Message)
        {
          console.log(Message);
        }


    function setupAjax() //body Onload - calls it.
    {
        var iframe = document.createElement("iframe");
        iframe.src = "adminpush.aspx"; 
        iframe.style.display = "none";
        document.body.appendChild(iframe);
    }

So basically the iframe is being injected with script comands , which updates the parent of the iframe which is index.html.

It is working.

But when I tested it - it stopped updating after 45 seconds.

I thought it had to do with requestTimeout prop in web.config - but it wasnt.

It was related to the missing AsyncTimeOut prop in the AdminPush.aspx page.

Question #1:

According to msdn AsyncTimeout :

Gets or sets a value indicating the time-out interval used when processing asynchronous tasks.

But it also says :

A TimeSpan that contains the allowed time interval for completion of the asynchronous task. The default time interval is 45 seconds.

please notice that I "delay" 2 sec every time

at first I set the timeout to 1 minute , but then it failed also. I thought that the timeout should be regarding each operation , and not to sum(all async operations)

Why is it like that ? it suppose to be timeout for async task ! (single) but it behaves as sum(tasks)

The wording here are misleading. any clarification ?

Question #2:

I need to set it to max value. what is that value ? but still , I need it so support a browser for a very long time. so I'm afraid that this value won't help either.

Is there any way I can RESET this value (after n cycles) ?

I know that there are other solutions/libraries like signalR which are doing the job, still, it does not prevent learning how other stuff are done.

Was it helpful?

Solution

The idea of Asynchronous Pages is to free IIS so more users can be served, if you create a page that "never" finishes, you will eat up all your resources.

That been said... if you still want to do it...

We "know" (documentation), Asynchronous Pages work by splitting the execution of the page in 2... everything BEFORE the Background Tasks and everything AFTER the tasks, in that way IIS can process more requests while the background tasks finish their work. (there is more to it, but that is enough for now)

So... they "must" be creating some kind of Task Manager (like a root/main task) that executes all the registered tasks in sequence, in that way IIS starts processing the page, fires up the task manager, frees IIS, the task manager keeps processing the tasks and when it finishes, it returns control to IIS.

That would explain why the AsyncTimeout controls all the registered tasks instead of one-by-one (The timeout is actually applied to the Task Manager).

I tested a variation of your code with a timeout of 6000 seconds and it works:

C#:

protected void Page_Load(object sender, EventArgs e)
{
    Page.RegisterAsyncTask(new PageAsyncTask(ProcessTask));
}

protected async Task ProcessTask()
{
    await Task.Delay(1000);
    Response.Write(DateTime.Now.ToLongTimeString() + "<br/>");
    Response.Flush();
    Page.RegisterAsyncTask(new PageAsyncTask(ProcessTask));
}

aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Sample03.Default" Async="true" AsyncTimeout="6000" %>

Hope it helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top