在ASP里,如果想声明一个全局的字符变量,当然可以放在session或者cookies或者application里。但如果这个变量是一个对象,如一个VB.net的DLL的类,比如这个类在做一些事情,时间比较久,并且前台通过ajax想要和这个DLL进行通信的话,就有些困难了,因为必须设置成为全局变量,不然当一次访问结束后,必定要重新新建一个对象。这里提供一种把对象放在session里的方法,经测试可以使用,但是可能是由于网速等方面的影响,有时会稍微有点卡机的现象,这个目前还没有解决,有待日后再继续研究。
set Session("Dll")=server.CreateObject(DllName)
这里的Session("Dll")必须使用SET的方法,不然的话,就会不能用,如
set dll = server.CreateObject(DllName)
Session("Dll")= dll
他会认为Session("Dll")是一个字符串
经过set Session("Dll")=server.CreateObject(DllName)建立的session就可以完全的使用DLL所有的属性及函数了。
如:
set Session("Dll")=server.CreateObject(DllName)
Session("Dll").name = "test"
Session("Dll").Start()
Response.Write Session("Dll").name
下面把所有的代码全部贴出来:
1、新建一个VB.net的DLL项目。
Public Class Class1
Public name As String = ""
Private count As Integer = 0
Public Function Start() As Boolean
name = "start"
Dim thread1 As New Threading.Thread(New Threading.ThreadStart(AddressOf open))
thread1.Start()
Return True
End Function
Public Sub open()
'System.IO.File.Create("E:/brightvision/8080-LiveOn-TMシート/SRC/web/Common/Dll/txt.txt")
While (count < 10000)
System.Threading.Thread.Sleep(1000)
name = "Open" + count.ToString()
count += 1
End While
End Sub
End Class
2、新建a.asp用于处理session
<%@ Language=VBScript %>
<%
Response.CacheControl = "no-cache"
Response.Expires = -1
Response.ContentType="text/html; charset=Shift_JIS"
if Session("Dll") = "" then
DllName="jugetest.Class1"
set Session("Dll")=server.CreateObject(DllName)
Session("Dll").name = "test"
Session("Dll").Start()
Response.Write Session("Dll").name
else
response.Write(Session("Dll").name )
end if
%>
3、新建b.asp用于创建ajax
<div id="text1"></div>
<script language=javascript>
//---------------------------FileList----------------------------
var xmlHttpFileList = false;
function CreateAjaxXmlHttpFileList()
{
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
try {
xmlHttpFileList = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlHttpFileList = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e2) {
xmlHttpFileList = false;
}
}
@end @*/
if (!xmlHttpFileList && typeof XMLHttpRequest != 'undefined')
{
xmlHttpFileList = new XMLHttpRequest();
}
}
function ShowMessageFileList()
{
if (typeof xmlHttpFileList != 'undefined'
&& xmlHttpFileList != null && xmlHttpFileList.readyState == 4)
{
var response = new String();
response = xmlHttpFileList.responseText;
text1.innerHTML = response;
setTimeout('KintaiDataGet()',1000);
}
}
function KintaiDataGet()
{
var url = "a.asp?random="+ Math.random() + "&strType=FileList";
if(url != "")
{
CreateAjaxXmlHttpFileList();
// Open a connection to the server
xmlHttpFileList.open("POST", url, true);
// Setup a function for the server to run when it's done
xmlHttpFileList.onreadystatechange = ShowMessageFileList
// Send the request
xmlHttpFileList.send(null);
}
}
KintaiDataGet()
</script>
把DLL编译成COM组件,并且注册后,在asp里就能够正常的调用。