在asp.net中使用线程的问题 [英] Problem in using the thread in asp.net

查看:75
本文介绍了在asp.net中使用线程的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有一个网站,使用Ajax计时器和线程在10秒内加载数据,但是当我打开站点时,服务器cpu的使用率显示高达95%,并且服务器中存在的所有域运行缓慢.
我的HTML代码是:

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: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:

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();
        }



我使用"GetGoldData"过程从sql中的7table获取数据.
请帮帮我.
谢谢.



I get data from 7table in sql with ''GetGoldData'' procedure.
please help me.
Thanks.

推荐答案

难怪.一个简单的答案是:由于HTTP的性质和局限性以及HTTP服务器和Web浏览器的操作,使用线程技术不适合ASP.NET.从Web浏览器向HTTP服务器发送请求到在浏览器中加载服务器生成的资源之间,一切都会发生.如果您的ASP.NET应用程序发生在这两个事件之间,则所有运行时都不会因此而延长.

您可以通过服务器从Web端使用Web应用程序使用的其他外部自定义服务来创建一些更高级的架构,但这是一个很长的故事(我现在不想进入它,因为我不知道您的最终选择目标,并且因为对于典型的Web托管提供商而言这是不可能的;您将需要内部服务器或成熟的云解决方案).

也许您的应用程序的最终目标很简单,可以通过一个更简单的解决方案来实现,而无需额外的线程.如果您对此进行了解释,则可能有机会获得一些有用的建议.

[针对后续讨论进行编辑]

在您描述的情况下,您可能应该继续使用Ajax计时器,而不是服务器端的线程.相反,您需要对来自客户端的请求执行数据库轮询.现在,由于您的请求可能过于频繁,因为按计时器进行轮询非常繁琐,并且不同的客户端发送了相同的请求,因此您实际上只需要对某些事件进行数据库轮询.

为此,您需要记录重发轮询中的时间戳,并将其与HTTP请求时的服务器时间进行比较.仅当上次实际轮询与HTTP请求时间之间的时间间隔足够大时,才应执行实际轮询.当您实际轮询时,也可以检测到与前一次轮询相比数据有变化,这也很好.您需要使用新数据创建整个页面的数据高速缓存,并且仅在数据库中的实际数据发生更改时才更新此高速缓存.此缓存应该是完全生成的页面或其主要片段存储在服务器上.在HTTP响应中,您应该始终从该缓存中发送数据,并且仅在完成实际轮询后才更新此缓存(如前所述,并非针对每个HTTP请求),并且仅当数据库中的实际数据已更改后才更新此缓存.上次民意调查.

这可能是最简单的解决方案.

Web技术限制的问题很困难.根本的方法是使用 push技术.您可以从此处开始了解涉及的内容:
http://en.wikipedia.org/wiki/Server-push [
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.



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


这篇关于在asp.net中使用线程的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆