asp 延 时 总 结
asp有个特点,就是一个页面(无传递值(get不一样,form可以一样))执行中,如果还有人再次打开该页面,处理程序会放到等待上次用户执行该页面完成才能继续执行。也就是多线程下,页面被打开用户数同时个数,只能是同时一个人。所以,要达到知道用户退出,服务器来发,必须不同页面(同一个页面需要传送参数也算不同页面),不然会卡住。<%@ Language="VBScript" codepage ="936" EnableSessionState=false %> 头部加了这个是突破线程的限制,那么应用程序池 web园 ,线程 为 1 也可以达到延时的效果。
asp无传递值时,打开同一个页面,这个页面必须等待上次用户打开页面加载完成了,才处理第二个人打开页面。
asp有传递值不一样(post可以一样),就不会等待。
ceshi.asp的代码:
<%@ Language="VBScript" codepage ="936" EnableSessionState=false %>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<%
'response.Cookies("aa") = 111
'Application.Lock()
'Application("aa") = "aa"
'Application.UnLock()
Set ASP2DLL = Server.CreateObject("ASP2DLL.Demo")
'set conn = ASP2DLL. sql2005("26E6A42653BC462\SQL2005", "test","test","test")
'set rs = ASP2DLL.rs()
'rs.open"select * from mingzi order by id desc",conn,1,1
'do while not rs.eof
' response.write rs("mingzi")&"<br>"
''rs.movenext
'loop
'rs.close
'set conn2 = ASP2DLL.sql2000sp4(fuwuqi, zhanghao, mima, data) '测试可以用
'ASP2DLL.hello(12)
ASP2DLL.IsClientConnectedPandu("aa")
Set ASP2DLL = Nothing
asp2dll---aspmod 代码:
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Public objContext As ObjectContext
Public Application As ASPTypeLibrary.Application
Public Server As ASPTypeLibrary.Server
Public Session As ASPTypeLibrary.Session
Public Response As ASPTypeLibrary.Response
Public Request As ASPTypeLibrary.Request
Public Sub ASP2DLL_Initialize()
On Error Resume Next
Set objContext = GetObjectContext
Set Application = objContext.Item("Application")
Set Server = objContext.Item("Server")
Set Session = objContext.Item("Session")
Set Request = objContext.Item("Request")
Set Response = objContext.Item("Response")
' If Application("IsClientConnectedPandu") = "" Then
' Application("IsClientConnectedPandu") = 1 'Response.Redirect ("?")
' Response.Write "<script>alert('程序初始化,可正常使用了');location.href='?'</script>"
'Server.Transfer ("?")
' Response.End
'End If '该过程是为第一次编译,第一次编译会卡住 IIS 访问进程
End Sub
Public Sub ASP2DLL_Terminate()
On Error Resume Next
Set Application = Nothing
Set Server = Nothing
Set Session = Nothing
Set Request = Nothing
Set Response = Nothing
Set objContext = Nothing
' Set Sleep = Nothing
End Sub
Private Sub Class_Initialize()
ASP2DLL_Initialize
End Sub
Private Sub Class_Terminate()
ASP2DLL_Terminate
End Sub
asp2dll---demo 代码:
Public Sub hello(val)
Response.Write ("Hello World!")
Dim i
For i = 0 To val
' Server.ScriptTimeout = 10
If Application("ceshi") = 1 Then
Exit For
End If
Response.Write i & "<br>"
Response.Flush
Delay (1)
Next
End Sub
Public Sub hello2(val)
Response.Write ("Hello World!")
Dim i
For i = 0 To val
' Server.ScriptTimeout = 10
If Application("ceshi") = 1 Then
Exit For
End If
Response.Write i & "<br>"
Response.Flush
Call Sleep(1000)
Next
End Sub
Public Function Delay(DT As Single)
'延时 DT 秒
'当 DT=0,返回Delay状态: True:未完成, False:已完成
Dim t1 As Single
Static InDelay As Boolean
'
If InDelay Or DT <= 0 Then
Delay = InDelay
Exit Function
End If
'
InDelay = True
t1 = Timer
Do Until Timer - t1 > DT
Call Sleep(10)
DoEvents
Loop
InDelay = False
Delay = False
End Function
Public Function sql2000sp4(fuwuqi, zhanghao, mima, data)
Dim conn
Set sql2000sp4 = Server.CreateObject("ADODB.CONNECTION")
'conn = "provider = sqloledb ; data source = (" & fuwuqi & ") ; uid = " & zhanghao & " ; pwd = " & mima & "; database = " & data
'错误 conn = "provider=sqloledb ;data source = (local) ;uid=" & zhanghao & " ;pwd=" & mima & ";database=" & data & ""
conn = "provider=sqloledb;data source=" & fuwuqi & ";uid=" & zhanghao & " ;pwd=" & mima & ";database=" & data & ""
'空格会引起连接数据库失败 错误写法: uid = " & zhanghao & " 正确写法:uid=" & zhanghao & "
sql2000sp4.open conn
End Function
Public Function sql2005(fuwuqi, zhanghao, mima, data)
Dim conn
Set sql2005 = Server.CreateObject("ADODB.CONNECTION")
'conn = "provider = sqloledb ; data source = (" & fuwuqi & ") ; uid = " & zhanghao & " ; pwd = " & mima & "; database = " & data
'错误 conn = "provider=sqloledb ;data source = (local) ;uid=" & zhanghao & " ;pwd=" & mima & ";database=" & data & ""
conn = "provider=sqloledb;data source=" & fuwuqi & ";uid=" & zhanghao & " ;pwd=" & mima & ";database=" & data & ""
'空格会引起连接数据库失败 错误写法: uid = " & zhanghao & " 正确写法:uid=" & zhanghao & "
sql2005.open conn
End Function
Public Function access(lujing)
Dim conn
Set access = Server.CreateObject("ADODB.CONNECTION")
conn = "provider=microsoft.jet.oledb.4.0;data source=" & lujing
access.open conn
End Function
Public Function rs()
Set rs = Server.CreateObject("adodb.recordset")
End Function
Public Function IsClientConnectedPandu(id_)
'控制打开的页面个数 开始
'If Application("dizeng" & id_) > 0 Then
' Response.Write "<script>alert('您打开的页面太多了,该页面只能打开一个');</script>"
' Exit Function
' End If
'控制打开的页面个数 结束了
Dim ii
ii = 0
Do While Response.IsClientConnected
'================================
'在这里修改或自定义要执行的ASP代码
ii = ii + 1
Response.Write ii & "--"
'Response.Write Application("dizeng") & "我晕..." & Application("dizeng" & id_)
'Application("dizeng" & id_) = Application("dizeng" & id_) + 1
' youdezhi = Application("dizeng" & id_)
'================================
Response.Flush
'
'Delay (1) '
Call Sleep(1000)
' Response.Clear
If Response.IsClientConnected = False Then
'Application("dizeng" & id_) = 0
'Response.Cookies("ceshi") = 1
End If
Loop
End Function
[ 本帖最后由 ysf0181 于 2014-3-3 15:18 编辑 ]