When object refuse to die 
Author Message
 When object refuse to die

Hypothetical situation:

While script is running (and it runs for a while copying files from place A to place B), script uses toolbar-less IE to inform user of what is going on. If browser info-window is closed through user intervention, script fails next time IE object access is attempted (as expected).

Unexpected: seems there is no way to detect this kind of situation and rectify it by instantiating new IE object. Killing IE or setting object to Nothing (in script) somehow in the eyes of script engine does not release object.
Simplified example:

Dim goIE
Set goIE = CreateObject("InternetExplorer.Application")
'.
'..
'... some code here

MsgBox "IsObject(goIE) = " & IsObject(goIE) ' TRUE indeed
Set goIE = Nothing                          ' Now I've "killed" object
MsgBox "IsObject(goIE) = " & IsObject(goIE) ' still TRUE (life after death?)

'
' later on

If IsObject(goIE) Then     ' since TRUE, unsuspectingly - I will try to use object
    goIE.Document.Open     ' as a consequence of previously setting object to Nothing - script fails!

I would speculate that object is truly released only when script stops, not before. This supports theory:

MsgBox "IsObject(goIE)" & IsObject(goIE) ' Here I had real - "alive" IE object,
Set goIE = Nothing                       ' Now I killed it.
MsgBox "IsObject(goIE)" & IsObject(goIE) ' MsgBox says it exists (and it does not),  
Set goIE = CreateObject("InternetExplorer.Application") ' now I try to create new instance (no errors - OK?),
MsgBox "IsObject(goIE)" & IsObject(goIE) ' MsgBox again confirms object existence,      
goIE.Document.Open                       ' Script fails here.
goIE.Document.Write CStr("Hi")

So I can not destroy IE object it and create it few lines later.

Is there a way to really kill IE object instance and then instantiating it again without having to stop the script? I am kin of lost on this one.

Branimir



Tue, 05 Feb 2002 03:00:00 GMT  
 When object refuse to die

Nothing is effectively a null object, so IsObject will return true.  Use TypeName instead which will return the string "Nothing" if the object variable has been set to nothing.

set ie = createobject("internetexplorer.application")
msgbox "ie isobject=" & isobject(ie) & vbcrlf _
     & "ie typename=" & typename(ie)  
set ie = nothing
msgbox "ie isobject=" & isobject(ie) & vbcrlf _
     & "ie typename=" & typename(ie)  

If the user kills IE then typename will change to "Object".

In the example above, comment out the "set ie = nothing" and close IE while the first message is displayed and you'll see...

--
Michael Harris

Hypothetical situation:

While script is running (and it runs for a while copying files from place A to place B), script uses toolbar-less IE to inform user of what is going on. If browser info-window is closed through user intervention, script fails next time IE object access is attempted (as expected).

Unexpected: seems there is no way to detect this kind of situation and rectify it by instantiating new IE object. Killing IE or setting object to Nothing (in script) somehow in the eyes of script engine does not release object.
Simplified example:

Dim goIE
Set goIE = CreateObject("InternetExplorer.Application")
'.
'..
'... some code here

MsgBox "IsObject(goIE) = " & IsObject(goIE) ' TRUE indeed
Set goIE = Nothing                          ' Now I've "killed" object
MsgBox "IsObject(goIE) = " & IsObject(goIE) ' still TRUE (life after death?)

'
' later on

If IsObject(goIE) Then     ' since TRUE, unsuspectingly - I will try to use object
    goIE.Document.Open     ' as a consequence of previously setting object to Nothing - script fails!

I would speculate that object is truly released only when script stops, not before. This supports theory:

MsgBox "IsObject(goIE)" & IsObject(goIE) ' Here I had real - "alive" IE object,
Set goIE = Nothing                       ' Now I killed it.
MsgBox "IsObject(goIE)" & IsObject(goIE) ' MsgBox says it exists (and it does not),  
Set goIE = CreateObject("InternetExplorer.Application") ' now I try to create new instance (no errors - OK?),
MsgBox "IsObject(goIE)" & IsObject(goIE) ' MsgBox again confirms object existence,      
goIE.Document.Open                       ' Script fails here.
goIE.Document.Write CStr("Hi")

So I can not destroy IE object it and create it few lines later.

Is there a way to really kill IE object instance and then instantiating it again without having to stop the script? I am kin of lost on this one.

Branimir



Tue, 05 Feb 2002 03:00:00 GMT  
 When object refuse to die

oops... helps to make ie visible

set ie = createobject("internetexplorer.application")
ie.visible = 1
msgbox "ie isobject=" & isobject(ie) & vbcrlf _
     & "ie typename=" & typename(ie)  
set ie = nothing
msgbox "ie isobject=" & isobject(ie) & vbcrlf _
     & "ie typename=" & typename(ie)  

--
Michael Harris

Nothing is effectively a null object, so IsObject will return true.  Use TypeName instead which will return the string "Nothing" if the object variable has been set to nothing.

set ie = createobject("internetexplorer.application")
msgbox "ie isobject=" & isobject(ie) & vbcrlf _
     & "ie typename=" & typename(ie)  
set ie = nothing
msgbox "ie isobject=" & isobject(ie) & vbcrlf _
     & "ie typename=" & typename(ie)  

If the user kills IE then typename will change to "Object".

In the example above, comment out the "set ie = nothing" and close IE while the first message is displayed and you'll see...

--
Michael Harris

Hypothetical situation:

While script is running (and it runs for a while copying files from place A to place B), script uses toolbar-less IE to inform user of what is going on. If browser info-window is closed through user intervention, script fails next time IE object access is attempted (as expected).

Unexpected: seems there is no way to detect this kind of situation and rectify it by instantiating new IE object. Killing IE or setting object to Nothing (in script) somehow in the eyes of script engine does not release object.
Simplified example:

Dim goIE
Set goIE = CreateObject("InternetExplorer.Application")
'.
'..
'... some code here

MsgBox "IsObject(goIE) = " & IsObject(goIE) ' TRUE indeed
Set goIE = Nothing                          ' Now I've "killed" object
MsgBox "IsObject(goIE) = " & IsObject(goIE) ' still TRUE (life after death?)

'
' later on

If IsObject(goIE) Then     ' since TRUE, unsuspectingly - I will try to use object
    goIE.Document.Open     ' as a consequence of previously setting object to Nothing - script fails!

I would speculate that object is truly released only when script stops, not before. This supports theory:

MsgBox "IsObject(goIE)" & IsObject(goIE) ' Here I had real - "alive" IE object,
Set goIE = Nothing                       ' Now I killed it.
MsgBox "IsObject(goIE)" & IsObject(goIE) ' MsgBox says it exists (and it does not),  
Set goIE = CreateObject("InternetExplorer.Application") ' now I try to create new instance (no errors - OK?),
MsgBox "IsObject(goIE)" & IsObject(goIE) ' MsgBox again confirms object existence,      
goIE.Document.Open                       ' Script fails here.
goIE.Document.Write CStr("Hi")

So I can not destroy IE object it and create it few lines later.

Is there a way to really kill IE object instance and then instantiating it again without having to stop the script? I am kin of lost on this one.

Branimir



Tue, 05 Feb 2002 03:00:00 GMT  
 When object refuse to die

Quite an eye opener! Works like a dream!! Thanks Michael!!!

Branimir

' ===================================================================
Option Explicit
Dim oIE

InitialiseIE
oIE.Visible = 1

Msgbox "IsObject(oIE)=" & IsObject(oIE) & vbcrlf _
     & "TypeName(oIE)=" & TypeName(oIE)      ' manually closed IE here
'Set oIE = Nothing ' <<< On/Off comment
Msgbox "IsObject(oIE)=" & IsObject(oIE) & vbcrlf _
     & "TypeName(oIE)=" & TypeName(oIE)      ' it sees closed IE as Object

If TypeName(oIE)="Object" OR TypeName(oIE)="Nothing" Then InitialiseIE
oIE.Document.Open
oIE.Document.Write "<b>Printing to IE</b>"
oIE.Document.Close
Do While oIE.Busy: Loop
oIE.Visible=1                                 ' risen from "dead"

Msgbox "IsObject(oIE)=" & IsObject(oIE) & vbcrlf _
     & "TypeName(oIE)=" & TypeName(oIE)  

oIE.Visible=0
Set oIE = Nothing

Wscript.Quit                                  ' no errors, no glitches!

' ===================================================================
Sub InitialiseIE()
 Set oIE = CreateObject("InternetExplorer.Application")

 If Err.Number = 0 Then
  With oIE
   .height=100
   .width=150
   .menubar=0
   .toolbar=0
   .navigate "About:Blank"
  End With
  Do while oIE.Busy: Loop
 End If
End Sub
' -------------------------------------------------------------------


  oops... helps to make ie visible

  set ie = createobject("internetexplorer.application")
  ie.visible = 1
  msgbox "ie isobject=" & isobject(ie) & vbcrlf _
       & "ie typename=" & typename(ie)  
  set ie = nothing
  msgbox "ie isobject=" & isobject(ie) & vbcrlf _
       & "ie typename=" & typename(ie)  

  --
  Michael Harris



Tue, 05 Feb 2002 03:00:00 GMT  
 When object refuse to die

Quote:
>Nothing is effectively a null object, so IsObject will return true.
>Use TypeName instead which will return the string "Nothing" if
>the object variable has been set to nothing.

What about:

    If (var Is Nothing) Then

It works in VB, and probably should work in VBScript (although I haven't
tried it). Nicer than checking the typename against a string literal...

Peter

--
Peter J. Torr - Microsoft Windows Script Program Manager

Please do not e-mail me with questions - post them to this
newsgroup instead. Thankyou!



Tue, 05 Feb 2002 03:00:00 GMT  
 When object refuse to die
Doesn't work if user closes IE (as opposed to ie variable set to nothing by
script code)...

set ie = createobject("internetexplorer.application")
ie.visible = 1
msgbox "ie isobject=" & isobject(ie) & vbcrlf _
     & "ie typename=" & typename(ie)
'set ie = nothing
msgbox "ie isobject=" & isobject(ie) & vbcrlf _
     & "ie typename=" & typename(ie)
'if user closes IE before the next statement
'then (ie is nothing) evaluates to false!
msgbox "ie Is Nothing =" & (ie is nothing)

--
Michael Harris





| >Nothing is effectively a null object, so IsObject will return true.
| >Use TypeName instead which will return the string "Nothing" if
| >the object variable has been set to nothing.
|
| What about:
|
|     If (var Is Nothing) Then
|
| It works in VB, and probably should work in VBScript (although I haven't
| tried it). Nicer than checking the typename against a string literal...
|
| Peter
|
| --
| Peter J. Torr - Microsoft Windows Script Program Manager

| Please do not e-mail me with questions - post them to this
| newsgroup instead. Thankyou!
|
|
|



Tue, 05 Feb 2002 03:00:00 GMT  
 When object refuse to die

One small point, when testing for object's existence using TypeName(oIE), it is wise to embed small pause in front of "If" block:

Wscript.Sleep 100    ' this is instead of VB's DoEvents, gives system time to "catch breath"
If NOT TypeName(oIE)= "Object" Then
    , some code using oIE
Else
   InitialiseIE
End If  

If oIE is closed manually shortly before upper code section tests for object existance, system does not have time to update object's status, and TypeName(oIE) still returns IWebBrowser2 as type name. Based on wrong answer, code crashes when tries to use oIE ('cause its not there - user closed it).

Inserting little 100ms pause gives system necessarry "breathing" room, and then function return reflects true situation - string "Object", to which script can properly react.

By the way testing IE object's existence with this:

If (oIE Is Nothing) Then
 MsgBox "oIE is Nothing"
Else
 MsgBox "oIE is NOT Nothing"
End If

works only if object is explicitly set to nothing, not if user just clicks and closes oIE's window.


  Quite an eye opener! Works like a dream!! Thanks Michael!!!

  Branimir

  ' ===================================================================
  Option Explicit
  Dim oIE

  InitialiseIE
  oIE.Visible = 1

  Msgbox "IsObject(oIE)=" & IsObject(oIE) & vbcrlf _
       & "TypeName(oIE)=" & TypeName(oIE)      ' manually closed IE here
  'Set oIE = Nothing ' <<< On/Off comment
  Msgbox "IsObject(oIE)=" & IsObject(oIE) & vbcrlf _
       & "TypeName(oIE)=" & TypeName(oIE)      ' it sees closed IE as Object

  If TypeName(oIE)="Object" OR TypeName(oIE)="Nothing" Then InitialiseIE
  oIE.Document.Open
  oIE.Document.Write "<b>Printing to IE</b>"
  oIE.Document.Close
  Do While oIE.Busy: Loop
  oIE.Visible=1                                 ' risen from "dead"

  Msgbox "IsObject(oIE)=" & IsObject(oIE) & vbcrlf _
       & "TypeName(oIE)=" & TypeName(oIE)  

  oIE.Visible=0
  Set oIE = Nothing

  Wscript.Quit                                  ' no errors, no glitches!

  ' ===================================================================
  Sub InitialiseIE()
   Set oIE = CreateObject("InternetExplorer.Application")

   If Err.Number = 0 Then
    With oIE
     .height=100
     .width=150
     .menubar=0
     .toolbar=0
     .navigate "About:Blank"
    End With
    Do while oIE.Busy: Loop
   End If
  End Sub
  ' -------------------------------------------------------------------


    oops... helps to make ie visible

    set ie = createobject("internetexplorer.application")
    ie.visible = 1
    msgbox "ie isobject=" & isobject(ie) & vbcrlf _
         & "ie typename=" & typename(ie)  
    set ie = nothing
    msgbox "ie isobject=" & isobject(ie) & vbcrlf _
         & "ie typename=" & typename(ie)  

    --
    Michael Harris



Wed, 06 Feb 2002 03:00:00 GMT  
 When object refuse to die

Quote:
> Doesn't work if user closes IE (as opposed to ie variable set to nothing
by
> script code)...

Ahhh, of course. Sorry, I missed the point of the discussion ;-)

It won't be Nothing because it is still a valid object reference, but it's
type would be Nothing because what is on the end of the object reference has
died.

Peter

--
Peter J. Torr - Microsoft Windows Script Program Manager

Please do not e-mail me with questions - post them to this
newsgroup instead. Thankyou!



Thu, 07 Feb 2002 03:00:00 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. CreateObject refuses to create remote Object

2. Cloned tbody object refuses to fire onclick event (addendum to previous post)

3. COM Object Instance Never Dies

4. Added object never dies

5. page counter refuses to work

6. gcc on ultrix4.2 refuses to build gs3.53

7. Wscript.shell refusing to run a command from an ASP page

8. permission refused

9. Excel refused Sendkeys why?

10. WScript refuses to sleep(WTF!?)

11. die Outlook e-mail adresse

12. Rerun a program automatically in wsh if it dies or is shutdown

 

 
Powered by phpBB® Forum Software