2014-10-08 21 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. 
相關問題