Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I've a website that using Ajax timer and Thread for load data in 10 seconds but when i open site the server cpu usage show upto 95% and all of domains than exist in server run slowly.
my Html code is:
ASP.NET
<asp:ScriptManager ID="ScriptManager2" runat="server" EnablePartialRendering="true">
</asp:ScriptManager>
<asp:Timer ID="Timer1" runat="server" Interval="10000" 
    OnTick="RefreshPanel_Tick" Enabled="False">
</asp:Timer>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
    </Triggers>
    <ContentTemplate>
    .................
    </ContentTemplate>
</asp:UpdatePanel>



and my code behind is:
C#
protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Timer1.Interval = 60000;
                Timer1.Enabled = true;
            }
        }

protected void RefreshPanel_Tick(object sender, EventArgs e)
        {
            System.Threading.Thread T = new System.Threading.Thread(new System.Threading.ThreadStart(GetGoldData));
            T.IsBackground = true;
            T.Start();
        }


I get data from 7table in sql with 'GetGoldData' procedure.
please help me.
Thanks.
Posted
Updated 26-Feb-12 22:14pm
v2
Comments
Tech Code Freak 27-Feb-12 4:14am    
Added pre tags

1 solution

No wonder. A quick answer is: using threading is not suitable to ASP.NET due to the nature and limitations of HTTP and the operation of HTTP server and a Web browser. Everything happens between a Web browser sending a request to a HTTP server and loading a resource generated by a server in a browser. All the runtime if your ASP.NET application happens between these two events, so it cannot be prolonged in time.

You can create some more advanced schema with additional external custom service consumed by your Web application from the server side, but this is a long story (I don't want to get into it right now because I don't know your ultimate goals and because this is not possible with a typical Web hosting provider; you would need an in-house server or a fully-fledged cloud solution).

Maybe the ultimate goal of your application is simple enough to be reached by a simpler solution, without an extra thread. If you explain it, you might get a chance to receive some useful advice.

[EDIT in response to follow-up discussion]

In the case you describe, you probably should continue using Ajax timer, but not the thread on the server side. Instead, you need to perform database polling on requests from the clients. Now, as you can have too frequent requests as polling by timer is very excessive and different clients send the same requests, you need to actually poll the database only on some of the events.

For this purpose, you need to record timestamp from the resent poll and compare it with the server time at the moment of the HTTP request. You should perform the actual poll only if the time span between the last actual poll and HTTP request time is big enough. When you actually poll, it would be also good it you could detect that there is a change in data compared to the previous poll. You need to create a data cache of the whole page with new data and update this cache only when actual data from the database change. This cache should be fully-generated page or its major fragment stored on the server. In HTTP response, you should always send the data from this cache, and update this cache only when the actual poll is done (as I explained before, not on every HTTP request) and only if actual data has been changed in the database since the last poll.

This is probably the simplest solution.

The problem of the limitation of Web technology is difficult. The radical approach would be using of the push technology. You can learn what's involved starting from here:
http://en.wikipedia.org/wiki/Server-push[^].

—SA
 
Share this answer
 
Comments
Hassan Sajedi 27-Feb-12 3:58am    
Thanks for your follow /
maybe the problem is simple and i need a simple solution for that . any way i try to explain everything for your :
We design a web site for viewing some prices for the other resources in our website . for example the price of gold and dollar and etc .There is database in our dedicated server that we read some data (prices) from 7 table every 10 seconds .we done this by ajax timer and threading. as this project start in our website , the cpu usage goes from 20%(normal situation) to 100 % and the server goes to suspended mode . this web site is www.tehrangold.net/default.aspx .
So how can we solve this problem instead of thread ?
Sergey Alexandrovich Kryukov 27-Feb-12 12:13pm    
Please see my updated answer, after [EDIT].
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900