2014-10-08 64 views
1

我做了简单的网页。当它用查询字符串调用时,它会增加应用程序变量。 3秒后,线程完成变量减少。更改应用程序变量没有给予正确的结果

问题是,当我打开2个浏览器页面(或mozilla)并从一个页面调用并从其他页面获取结果时,一切正常。但是当我使用多线程测试应用程序拨打200或更多的电话时,号码不会回到零。它不会像240或20一样保持相同的数字。每次它不同但不是零。

page_load事件准备自动化进程。我的多线程应用程序调用此页面,如http://localhost:4444/test.aspx?i=anything,因此页面automaticallu将1添加到变量“x”并启动线程。变量应该在3秒后减1,总数应该为零。但不是 ???

重要:当我在我的测试应用程序中使用1个线程来调用此网页时,它“起作用”很好。例如,它有时会达到680并返回到零。如果我使用2个线程来调用页面,那么结果总是“不一致”。

有趣的是不是如果我使用SQL Server,并保持变量存在,并增加与

UPDATE test SET count=count + 1 
UPDATE test SET count=count - 1 

我看不出有什么问题,即使有20个并发连接/减少使用应用程序变量。

有什么建议吗?

Partial Class test 
    Inherits System.Web.UI.Page 
    Dim _resetEvent As New AutoResetEvent(False) 
    Protected Sub cmdWriteVariable_Click(sender As Object, e As EventArgs) Handles cmdWriteVariable.Click 
     Response.Write(Application("x").ToString) 
    End Sub 
    Protected Sub cmdIncreaseValue_Click(sender As Object, e As EventArgs) Handles cmdIncreaseValue.Click 
     If Application("x") Is Nothing Then Application("x") = 0 
     Application.Lock() 
     Application("x") = CInt(Application("x").ToString) + 1 
     Application.UnLock() 
     Response.Write(Application("x").ToString) 
    End Sub 
    Protected Sub Async_start() 
     Dim workerObject As New myTestAsyncClass(_resetEvent) 
     workerObject.ad = "anyname" 
     Dim ctx As HttpContext = HttpContext.Current 
     Dim workerThread As New Thread(New ThreadStart(Sub() 
                  HttpContext.Current = ctx 
                  workerObject.DoWork2(ctx) 
                 End Sub)) 
     workerThread.Start() 
    End Sub 
    Protected Sub cmdReset_Click(sender As Object, e As EventArgs) Handles cmdReset.Click 
     Application("x") = 0 
    End Sub 
    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load 
     If Application("x") Is Nothing Then Application("x") = 0 

     If Request.QueryString("i") IsNot Nothing Then 
      Application.Lock() 
      Application("x") = CInt(Application("x").ToString) + 1 
      Application.UnLock() 
      Async_start() 
     End If 
    End Sub 
End Class 

,这是线程类:

Imports Microsoft.VisualBasic 
Imports System.Threading 

Public Class myTestAsyncClass 

    Dim _resetEvent As AutoResetEvent = Nothing 
    Public ad As String 

    Public Sub New(resetEvent As AutoResetEvent) 
     _resetEvent = resetEvent 
    End Sub 
    Public Sub DoWork2(state As Object) 
     Try 
      Dim context As HttpContext = TryCast(state, HttpContext) 
      System.Threading.Thread.Sleep(3000) 
      Dim x As Integer = CInt(context.Application("x").ToString) 
      x = x - 1 
      context.Application.Lock() 
      context.Application("x") = x.ToString 
      context.Application.UnLock() 
     Finally 
      _resetEvent.[Set]() 
     End Try 
    End Sub 
End Class 

和aspx页面,以防万一:其简单的3个按键。写入变量,重置为零并增加1.打开2个浏览器,单击多次增加并从其他浏览器单击写入变量按钮。你会看到它的工作正常。可以说10和3秒后变量变为零。

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="test.aspx.vb" Async="true" Inherits="test" %> 
<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
     <asp:Button ID="cmdIncreaseValue" runat="server" Text="increasevalue" /> 
     <br /> 
     <br /> 
     <asp:Button ID="cmdWriteVariable" runat="server" Text="write app variable" /> 
     <br /> 
     <br /> 
     <asp:Button ID="cmdReset" runat="server" Text="set varable to 0" /> 
    </div> 
    </form> 
</body> 
</html> 

回答

0

嗯,我相信我在这里找到:

我希望有一天它可以帮助别人。

最好..

http://stackoverflow.com/questions/25358448/why-my-httpcontext-application-variable-cannot-be-access-through-different-actio 

说:

Disadvantages of Application State: 

Application scope The scope of application state can also be a disadvantage. 
Variables stored in application state are global only to the particular process the 
application is running in, and each application process can have different values. 
Therefore, you cannot rely on application state to store unique values or update global 
counters in Web-garden and Web-farm server configurations.