FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour Intermittent Error
Posts: 933
Joined: Sun Oct 09, 2005 01:05 PM
Intermittent Error
Posted: Wed Aug 18, 2010 07:35 PM
Hi,

I have a routine that will check to see if MS-Excel is running and if so, terminate it.

It works about 95% of the time but I get an intermittent error pop up every now and then.

Here is the code I use:
Code (fw): Select all Collapse
FUNCTION WMIService()
   static oWMI
   local oLoc
   if oWMI == nil
      oLoc  := CREATEOBJECT( "wbemScripting.SwbemLocator" )
      oWMI  := oLoc:ConnectServer()
   endif
return oWMI

FUNCTION KillExcel()
   local oWmi, oList, oProc
   IF UPPER(ALLTRIM( cWinVersion() )) = "VISTA"
      Return Nil
   ENDIF
   Syswait(.01)
   oWmi     := WmiService()
   oList    := oWmi:ExecQuery( "select * from Win32_Process" )
   for each oProc in oList
         if oProc:Name = "EXCEL.EXE"
            oProc:Terminate()
            Syswait(1)
         endif
   next
return nil


The error that pops up is:


Error description: Error wbemScripting.SwbemLocator/16389 E_FAIL: CONNECTSERVER
Args:

Stack Calls
===========
Called from: win32ole.prg => TOLEAUTO:CONNECTSERVER(0)
Called from: EzSat.prg => WMISERVICE(20820)
Called from: EzSat.prg => KILLEXCEL(20831)


Anyone have any ideas as to why I get this error?
Thanks,

Jeff Barnes



(FWH 16.11, xHarbour 1.2.3, Bcc730)
Posts: 824
Joined: Thu Oct 13, 2005 07:39 AM
Re: Intermittent Error
Posted: Thu Aug 19, 2010 07:58 AM
Jeff,

you can use TRY..CATCH to avoid the error message

Code (fw): Select all Collapse
TRY
    oLoc := CreateObject( "wbemScripting.SwbemLocator" )
  CATCH oErr
    TRY
       oLoc := CreateObject( "wbemScripting.SwbemLocator" )
    CATCH oErr
        oLoc := nil
    END
END
kind regards

Stefan
Posts: 2365
Joined: Wed Nov 02, 2005 11:46 PM
Re: Intermittent Error
Posted: Thu Aug 19, 2010 11:53 AM

Jeff

what do you use, Harbour, xHarbour?

Posts: 933
Joined: Sun Oct 09, 2005 01:05 PM
Re: Intermittent Error
Posted: Thu Aug 19, 2010 11:59 AM

Thanks Stefan, I will try your idea to see if it helps :)

Daniel, I use xHarbour.

Thanks,

Jeff Barnes



(FWH 16.11, xHarbour 1.2.3, Bcc730)
Posts: 2365
Joined: Wed Nov 02, 2005 11:46 PM
Re: Intermittent Error
Posted: Thu Aug 19, 2010 12:02 PM
try
Code (fw): Select all Collapse
#include "fivewin.ch"

function Main

   LOCAL oWnd, oTimer
   
   define window oWnd 
   
   define timer oTimer interval 5000 action VerifyExcel( oTimer, oWnd ) of oWnd
   
   activate window oWnd on init oTimer:activate()


return nil

function VerifyExcel( oTimer, oWnd )
   
   local oExcel, oError

   TRY
      oExcel   = GetActiveObject( "Excel.Application" )
      oExcel:Quit()
   CATCH oError
   END   
   
return nil
Posts: 933
Joined: Sun Oct 09, 2005 01:05 PM
Re: Intermittent Error
Posted: Thu Aug 19, 2010 12:28 PM

Thanks Daniel,

This works if only one instance of Excel is open but what if there are more than one?

Thanks,

Jeff Barnes



(FWH 16.11, xHarbour 1.2.3, Bcc730)
Posts: 2365
Joined: Wed Nov 02, 2005 11:46 PM
Re: Intermittent Error
Posted: Thu Aug 19, 2010 12:47 PM
Jeff

Code (fw): Select all Collapse
#include "fivewin.ch"

function Main

   LOCAL oWnd, oTimer
   
   define window oWnd 
   
   define timer oTimer interval 100 action VerifyExcel( oTimer, oWnd ) of oWnd
   
   activate window oWnd on init oTimer:activate()


return nil

function VerifyExcel( oTimer, oWnd )
   
   local oExcel, oError,  lExit := .F.
   
   TRY
      oExcel   = GetActiveObject( "Excel.Application" )
      oExcel:Quit()
      oWnd:cTitle = time()
      Sleep( 1000 )
   CATCH oError
   END   
   
return nil

Continue the discussion