ASP.NET 锁定方法
Lock 方法阻止其他用户修改存储在应用程序对象中的变量。 它确保一次只有一个客户端可以修改应用程序变量。
ASP.NET 解锁方法
Unlock 方法使其他用户可以在使用 Lock 方法锁定应用程序对象后修改存储在应用程序对象中的变量。 如果用户未显式调用 Application.Unlock 方法,则服务器会在 .asp 文件结束或者超时时解锁锁定的应用程序对象。
在下面的例子中,我们使用 lock 方法阻止多个用户一次访问变量页面计数器,然后使用 unlock 方法允许其他用户访问应用程序中的变量。
default.apsx(锁定方法)
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!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 id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
The page has been visited
<asp:Label ID="myLabel" runat="server" />
times!
</div>
</form>
</body>
</html>
Default.aspx.vb
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Load
If Application("PageCounter") >= 10 Then
Application.Remove("PageCounter")
End If
If Application("PageCounter") Is Nothing Then
Else
Application.Lock()
Application.UnLock()
End If
myLabel.Text = Application("PageCounter")
End Sub
End Class
default.apsx(解锁方法)
<%@ Page Language="vb" %>
<html>
<head>
<script runat="server">
Sub Page_Load()
Application.Lock()
Application("Counter") = 1
Application.UnLock()
Message.Text = "Counter = " & Application("Counter")
End Sub
</script>
</head>
<body>
<asp:label id="Message" runat="server"/>
</body>
</html>
注意:页面无需锁定应用程序对象以编辑应用程序集合。
如果一个页面尝试编辑应用程序集合而无需锁定,并且在第二页也尝试编辑集合,IIS也不会发送错误,并且应用程序对象以不一致的状态结尾。
在本文中,您将了解 ASP.NET 中的 Application.Lock 和 Unlock 方法。
日期:2020-06-02 22:14:13 来源:oir作者:oir
