简体   繁体   中英

JQuery Retrieving Data from aspx page

I want retrieve data from aspx page. But, waiting data in static field if filling.

JQuery :

   $.ajax({
        url: "Services/JSON/GetMessage.aspx",
        type: "GET",
        dataType: "json",
        success: function (response) { alert(response.Text); },
        error: function (x, t, m) {
            if (t === "timeout") {
                alert("got timeout");
            } else {
                alert(t);
            }
        }
    })

Services/JSON/GetMessage.aspx

<%@ Page Language="C#" %>
<% 
Response.Clear();
Response.ContentType = "text/json";
int tryCount = 0;
while (1 == 1)
{
    if (ClsStaticFields.Messages.Count > 0)
    {
        foreach(string message in ClsStaticFields.Messages) {
             Response.Write("{ Text:'" + message + "' }");
        }
    }
    tryCount ++;
    if (tryCount > 29) break; // 1 Minute wait and exit
    System.Threading.Thread.Sleep(2000); // 2 Second wait
}
Response.End();
%>

I was curious:

  • is this waiting period affects other users?
  • is this cause locks on IIS or pages ?
  • is this usage effecting badly to CPU ?

I see that you use GetMessage.aspx page, an aspx page that is probably connected with session and this affect other users.

Disable the session for that page if this is possible and all is ok.

<%@ Page Language="C#" EnableSessionState="false" %>

relative:

Web app blocked while processing another web app on sharing same session
What perfmon counters are useful for identifying ASP.NET bottlenecks?
Replacing ASP.Net's session entirely

The full view

I do not think that is good idea the way you have make your waiting for the message inside this close loop and I suggest two alternatives.

Make the periodical call on browser using a javascript timer . Ether way you get the messages every 2 seconds, so why not call for that message from browser every couple of seconds ?

Or even better to use the comet method to call the server. Here is an example of comet in asp.net : http://www.aaronlerch.com/blog/2007/07/08/creating-comet-applications-with-aspnet/

Why is not good, first you use thread that belong to asp.net iis, and is connected with the browser call, that is leave an open connection and very soon your open connections will be run out, maybe also the asp.net net threads have problem with that.

Periodically check like yours is better to be done using Timers and not close loops with thread waits, and here the best place for that is the browser him self.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM