Click to See Complete Forum and Search --> : ASP and Unload/Terminate event?
Al Smith
Apr 26th, 2000, 02:04 AM
Hi,
I have an ASP application that creates some objects. Is there a way to quit and set the objects to nothing if the application is closed via the [X] box? (Similar to a form_unload or form_terminate event)
Thanks,
Al.
Well, there's no [X] box or any other visual components in an ASP application. So here's possibly an answer to conceptually what you're asking: set your objects to nothing at the end of the ASP script. example:
<%
' Start of your ASP Application
Dim objTest, strYourReturnValue
Set objTest = Server.CreateObject ("Project.Class")
strYourReturnValue = objTest.DoSomething
' Your done with any ASP processing and need to
' return to the browser(for example only)
' Clean Up
Set objTest = Nothing
' The next line is the last line to be executed
' in this ASP application example
Response.Redirect "somepage.asp?YourReturnValue=" & strYourReturnValue
%>
Al Smith
Apr 26th, 2000, 06:06 AM
The object that I create is an OLE interface to the company mainframe. This has to be active throughout the ASP application. There are several pages returned to the browser depending on the input received. For example, the user might request the page for item information on a part. He would enter the part number and the ASP application would return the description, price, availability, etc.
He then might select the order status page. He would enter the order number and the ASP application would return the shipped date, how it was shipped, expected arrival date, the tracking number, etc.
Now for my problem.
When the user is done he selects the [X] on the browser. This is when I want to set the OLE interface object to nothing. I've been playing with the IsClientConnected response event but I can't seem to get it to work.
Any thoughts or ideas will be greatly appreciated.
Thanks,
Al.
I'm not sure I know what you're defining as an OLE Interface Object but maybe this will help clarify what's happening.
Microsoft Help:"An ASP-based application is made up of all the files in its root directory and in any subdirectories. An application starts the first time that a user opens one of the Web pages in the application and ends when the server shuts down. The application has two events, an Application_OnStart event and an Application_OnEnd event."
So in terms of what you're describing as the purpose for the ASP pages, every time you request an asp page to do something, it will just do it's job. At the end of the script(when that specific job is done) you should set all of the objects that script created to nothing and return whatever information was requested.
The actual ASP Application never ends unless the web server is shut down, the clients do not control that, although hacking might influence the need for the web server to be shut down.
Is what you call an OLE Interface an ActiveX Document that users install on their machine locally? Maybe describe a little more about this OLE Interface... As far as the ASP page goes any objects it creates are usually dereferenced at the end of the script by default but for safety's sake you should probably set those to nothing.
IsClientConnected is used for to test whether or not the user who originally requested the asp page has is still around waiting for the return info or if they have gone to another page. Sometimes when you request a page and it takes too long you go to another page or press stop, IsClientConnected is supposed to return information about that, but this is something that the server script will process and the client won't see.
Because it's generally a bad idea to put any objects in the ASP Application object/scope, hopefully this is not where you are instantiating your OLE Object...
OmegaJunior
May 1st, 2000, 04:37 PM
Greetings,
Keeping objects alive and destroying them when the application stops... it sounds like a good time to discuss the global.asa in depth.
A previous comment stated this, so excuse me for repeating:
The global.asa starts when the first visitor opens a web page in an asp application. It stops when the last visitor has quit the asp application. Quitting can be done by abandoning the application or by timing out.
Thus, create and kill the object inside the global.asa. Open and close it in the pages where you need it. Don't set it to nothing from the pages. Opening and closing may be troublesome. I created a seperate function to check database connections and recordsets.
How to create and kill objects in the global.asa?
Use the <object> declaration, like this:
<OBJECT RUNAT=Server SCOPE=Session ID=BaanObj PROGID="Baan4.Application"></OBJECT>
This has to be the first line in the global.asa, before any script delimiters! All object declarations sit at the top of the page. You can have as many objects as memory allows, just remember to start their ID with a CAPITAL.
When this is done, you don't need to create it in the pages. If you do, the application would probable fail.
Because the object Scope is set to session, it will be instantiated for every single user that needs it. Thus a change in one instance won't affect another. Object Scope can be set to application as well, (only one at the time!), then changes in the instance affects all users, and it slows down performance.
Some objects do need some initialisation. From your code, it doesn't seem like Baan4.Application needs this. For objects that do, like an ADODB.Connection, initialise them in the Session_OnStart event, like so:
Sub Session_OnStart
If Not BaanObj Is Nothing Then
BaanObj.I_Dont_Know SomeParameter, SomeValue
End If
End Sub
To kill the object, in the global.asa, you actually don't need to do anything. Objects run out of scope when the user quits the asp application. If you do want to kill it, put the following code in the Session_OnEnd event:
Sub Session_OnEnd
If Not BaanObj Is Nothing Then
Set BaanObj = Nothing
End If
End Sub
You shouldn't use BaanObj functions there, it would take a lot of checking to make everything work correctly. That means that the Baan4.Application should check whether it has quit when the termination event kicks in.
How to use the object in the pages?
Normally, to use the object, you'd state
Set BaanObj = Server.CreateObject("Baan4.Application")
Now, because the object already exists, you mustn't create it anew. Simply check to see if the object exists:
If Not BaanObj Is Nothing Then 'it exists
BaanObj.ParseExecFunction Something
BaanObj.Quit
'Don't set it to nothing! Let the global.asa take care of memory!
End If
Remember the Error handling. In ASP there is no normal Error handling. All you can use is On Error Resume Next -- or not. If you do this, you might want to include an error routine:
If Err.Number <> 0 Then
'first stop the object
If Not BaanObj Is Nothing Then
If BaanObj.HasQuit = False Then
BaanObj.Quit
End If
End If
'then continue with error handling
End If
In VB, if the Baan4.Application errs, the VB Err object returns the automation error. ASP/VBScript doesn't do that. You need to build in safeguards to test whether or not execution was successful, like this:
Dim LngResult
LngResult = 0 'default
If Not BaanObj Is Nothing Then
BaanObj.ParseExecFunction SomeParameters, LngResult
If LngResult <> 0 Then
'something went wrong,
'recognizable by the value of LngResult
Else
'all went well
End If
BaanObj.Quit
End If
What if BaanObj Is Nothing?
You saw I always check the object before using it. If it is nothing, I cannot use it. You may want to redirect visitors to another page, reflect the error and let them start over. That's not such a visitor friendly move.
You also may create a function that checks whether the BaanObj is allright, and if not, reinitialise it. To avoid redundant code (repeating function code in all pages) you may want to put such a function into an include file. Let the function return a result, so by that result your page can decide to continue processing or stop.
I know this is a bit lengthy, but if I had someone explain this to me, I wouldn't have spent a year trying to figure it out.
Hope this helps!
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.