Application.Lock的作用

  • 9727
  • 0

摘要:Application.Lock的作用

這篇文章主要是講解Application.Lock的作用,並實驗看看他是否真的有效

首先我們先不使用Application.Lock,來測看看按下去的時間

首先是前端頁面Demo.aspx

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Thread.aspx.vb" Inherits="Thread" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>未命名頁面</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" Text="測試1" />
        <asp:Button ID="Button2" runat="server" Text="測試2" /></div>
    </form>
</body>
</html>

 

再來是後端程式碼Demo.aspx.vb

Page_Load

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Me.IsPostBack Then
            Me.Application("test") = 1
        End If

    End Sub

 

Button1_Click

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

        
        For int As Integer = 1 To 5
            System.Threading.Thread.Sleep(1000)
            If int = 5 Then
                Me.Application("test") -= 1
            End If
        Next
        

        Response.Write("Time=" & Now.ToLongTimeString & "<br />")
        Response.Write("1=" & Me.Application("test"))

    End Sub

 

 

 Button2_Click

    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
        
        For int As Integer = 1 To 5
            System.Threading.Thread.Sleep(1000)
            If int = 5 Then
                Me.Application("test") -= 1
            End If
        Next
        

        Response.Write("Time=" & Now.ToLongTimeString & "<br />")
        Response.Write("2=" & Me.Application("test"))
    End Sub

 

 

測試如果第一個視窗按下測試1的按鈕,第二個視窗同時按下測試2的按鈕,則兩個印出來的時間會相差一秒

 

改一下程式

Button1_Click

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

        Application.Lock()
        For int As Integer = 1 To 5
            System.Threading.Thread.Sleep(1000)
            If int = 5 Then
                Me.Application("test") -= 1
            End If
        Next
        Application.UnLock()

        Response.Write("Time=" & Now.ToLongTimeString & "<br />")
        Response.Write("1=" & Me.Application("test"))

    End Sub

 

 Button2_Click

    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
        Application.Lock()
        For int As Integer = 1 To 5
            System.Threading.Thread.Sleep(1000)
            If int = 5 Then
                Me.Application("test") -= 1
            End If
        Next
        Application.UnLock()

        Response.Write("Time=" & Now.ToLongTimeString & "<br />")
        Response.Write("2=" & Me.Application("test"))
    End Sub

 

再測試同時按,一個按測試1一個按測試2,但測試2的會晚五秒,這樣應該就可以証明Application.Lock是確實有用的

以上