Jump to content

only one program will run inside if then


leo8888
 Share

Recommended Posts

I have been trying for hours to figure out why this script will not work. It is a small gui script that I keep running on one of my terminal servers. It is supposed to allow a user to reset a quickbooks web connector when it gets frozen without waiting for me to do it for them if I am not available at that time.

The user runs a simple program from their desktop that does nothing more than create a text file on a shared folder on the server. The server script checks for existence of this file then kills and restarts the frozen connector.

When I test the script by dropping the trigger file in the shared folder it will kill the web connector process as expected and update the counter on the gui window but the second command that restarts the connector does nothing.

If I then drop the trigger file in the folder a second time the script will again update the counter and this time the command that restarts the web connector will work.

So basically it is acting as a kind of "toggle" changing the running state of the web connector program each time. Depending on if the web connector program is already running it will either kill the program or restart it but never both.

I only inserted the message box so I could verify that each line in the if - then block was executing and to add a short pause.

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Outfile=WebConnectorResetMonitor.exe
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <GuiConstants.au3>
#include <GUIConstantsEx.au3>
#include <EditConstants.au3>
#include <MsgBoxConstants.au3>
#include <Date.au3>


Opt("GUIOnEventMode", 1) ; Change to OnEvent mode

local $MainGui = GuiCreate("Quickbooks Web Connector Reset Monitor",450,150)
GUISetOnEvent($GUI_EVENT_CLOSE, "CancelButton")
local $iCancelButton = GuiCtrlCreateButton("End Monitor",150,50,100,25)
local $iLastReset = GUICtrlCreateLabel ( "Waiting for reset request...", 50, 100, 400, 25)
local $iResetCount = 0
GUICtrlSetFont(-1, 12)
GUICtrlSetOnEvent($iCancelButton, "CancelButton")
GUISetState(@SW_SHOW, $MainGui)

While 1
    Sleep(100) ; Sleep to reduce CPU usage
    If FileExists("e:\QuickBooks\ResetWebConnector.txt") Then ; Check for trigger file
      ShellExecute("cmd.exe", "/c taskkill /IM qbwc.exe /F") ; Kill the QuickBooks Web Connector
      MsgBox($MB_SYSTEMMODAL, "Restarting Web Connector", "The Web Connector will restart in 5 seconds or click OK to start it now.", 5)
      ShellExecute("C:\Program Files (x86)\ServiceTitan\ServiceTitan QBWC\QBWC.exe") ; Restart the QuickBooks Web Connector
      $iResetCount = $iResetCount + 1
      GUICtrlSetData( $iLastReset, "Last Reset: " & _Now() & "  Count: " & $iResetCount )
      FileDelete("e:\QuickBooks\ResetWebConnector.txt") ; Delete trigger file
    EndIf
WEnd

Func CancelButton()
  Exit
EndFunc   ;==>CancelButton

GuiDelete($MainGui)

 

Link to comment
Share on other sites

  • Replies 63
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Posted Images

1 minute ago, Nine said:

Maybe adding some error handling could help you understand what is the problem ?

Thank you, that is a good suggestion. I'm just not sure where to start.

I was looking for a way to step through a script with SciTE and maybe view return codes for each command but I do not see a way to do that.

Link to comment
Share on other sites

Link to comment
Share on other sites

2 hours ago, leo8888 said:

Depending on if the web connector program is already running it will either kill the program or restart it but never both.

Sounds like a timing issue, why not just wait for the process to close before restarting:

While 1
    Sleep(100) ; Sleep to reduce CPU usage
    If FileExists("e:\QuickBooks\ResetWebConnector.txt") Then ; Check for trigger file
      ShellExecute("cmd.exe", "/c taskkill /IM qbwc.exe /F") ; Kill the QuickBooks Web Connector
      ProcessWaitClose("qbwc.exe")
      ShellExecute("C:\Program Files (x86)\ServiceTitan\ServiceTitan QBWC\QBWC.exe") ; Restart the QuickBooks Web Connector
      $iResetCount = $iResetCount + 1
      GUICtrlSetData( $iLastReset, "Last Reset: " & _Now() & "  Count: " & $iResetCount )
      FileDelete("e:\QuickBooks\ResetWebConnector.txt") ; Delete trigger file
    EndIf
WEnd

 

Edited by JockoDundee
Changed waitproccessclose to ProcessWaitClose

Code hard, but don’t hard code...

Link to comment
Share on other sites

Link to comment
Share on other sites

3 minutes ago, Nine said:

A RunWait would simpler (like in my example) and no need for cmd.exe.  It is possible to call TaskKill directly.  I thought the example was explicit enough ;)

Well, RunWait is only waiting for the process Taskkill to end.  AFAIK, it, like AutoIt’s ProcessClose() does not necessarily wait until the process is 100% closed.  
It’s best therefore to make sure the process is closed.

Notepad.exe will likely close quickly enough to pass your test.

Code hard, but don’t hard code...

Link to comment
Share on other sites

19 minutes ago, JockoDundee said:

It’s best therefore to make sure the process is closed.

I think TaskKill waits until the process is closed with /F. 

It is false.  I found a situation where the process was still running after TaskKill /F.

So therefore, it is recommended to add ProcessWaitClose.

Edited by Nine
Link to comment
Share on other sites

Here is my updated code. Please excuse all the lines commented out. I'm still trying different things.

When I run the script now and drop my trigger file I can immediately see the web connector program disappear and the return code shows 0 on the gui.

Then after the 5 second pause I see the gui update to show the last rest count but the web connector still does not start and the return code is still zero.

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Outfile=WebConnectorResetMonitor.exe
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <GuiConstants.au3>
#include <GUIConstantsEx.au3>
#include <EditConstants.au3>
#include <MsgBoxConstants.au3>
#include <Date.au3>
#include <Constants.au3>

Opt ("MustDeclareVars", 1)

Opt("GUIOnEventMode", 1) ; Change to OnEvent mode

local $MainGui = GuiCreate("Quickbooks Web Connector Reset Monitor",450,150)
GUISetOnEvent($GUI_EVENT_CLOSE, "CancelButton")
local $iCancelButton = GuiCtrlCreateButton("End Monitor",150,50,100,25)
local $iLastReset = GUICtrlCreateLabel ( "Waiting for reset request...", 50, 100, 400, 25)
local $iReturnCode = GUICtrlCreateLabel ( "Return codes will display here...", 50, 120, 400, 25)
local $iResetCount = 0
GUICtrlSetFont(-1, 12)
GUICtrlSetOnEvent($iCancelButton, "CancelButton")
GUISetState(@SW_SHOW, $MainGui)

While 1
    Sleep(100) ; Sleep to reduce CPU usage
    If FileExists("e:\QuickBooks\ResetWebConnector.txt") Then ; Check for trigger file
      ;ShellExecute("cmd.exe", "/c taskkill /IM qbwc.exe /F") ; Kill the QuickBooks Web Connector
      Local $iRet = RunWait ("TaskKill /IM qbwc.exe /F")
      GUICtrlSetData( $iReturnCode, "Last Return Code: " & $iRet )
      Sleep(5000)
      ;ConsoleWrite ($iRet & "/" & @error & @CRLF)
      ;MsgBox($MB_SYSTEMMODAL, "Restarting Web Connector", "The Web Connector will restart in 5 seconds or click OK to start it now.", 5)
      ;ShellExecute("C:\Program Files (x86)\ServiceTitan\ServiceTitan QBWC\QBWC.exe") ; Restart the QuickBooks Web Connector
      Local $iRet = RunWait ("C:\Program Files (x86)\ServiceTitan\ServiceTitan QBWC\QBWC.exe")
      GUICtrlSetData( $iReturnCode, "Last Return Code: " & $iRet )
      ;ConsoleWrite ($iRet & "/" & @error & @CRLF)
      Sleep(5000)
      $iResetCount = $iResetCount + 1
      GUICtrlSetData( $iLastReset, "Last Reset: " & _Now() & "  Count: " & $iResetCount )
      FileDelete("e:\QuickBooks\ResetWebConnector.txt") ; Delete trigger file
    EndIf
WEnd

Func CancelButton()
  Exit
EndFunc   ;==>CancelButton

GuiDelete($MainGui)

 

Link to comment
Share on other sites

You didn't follow my suggestion.  Now, you cannot copy/paste the whole console info gathered with ConsoleWrite.  How hard can it be ?

1 hour ago, leo8888 said:

Local $iRet = RunWait ("C:\Program Files (x86)\ServiceTitan\ServiceTitan QBWC\QBWC.exe")

Should not be a RunWait, because if it is working, it will wait until the program QBWC ends before the script continues.  Add some "litterals" inside ConsoleWrite so you know which statement has been executed.

ps. Sorry to say, but if you do not want to use ConsoleWrite as I asked you, I am afraid I will not be able to help you any more.

 

Link to comment
Share on other sites

1 hour ago, leo8888 said:

Then after the 5 second pause I see the gui update

leo, stop guessing.  Put in a ProcessWaitClose() after the kill, as shown above.  Or a loop with ProcessExists()

Some programs have a lot of deallocation of memory, as well as waiting for various threads with sockets to close before they will actually start again.

And yes, per @Nine, add debug output if you do nothing else.
 

Code hard, but don’t hard code...

Link to comment
Share on other sites

15 hours ago, Nine said:

You didn't follow my suggestion.  Now, you cannot copy/paste the whole console info gathered with ConsoleWrite.  How hard can it be ?

Should not be a RunWait, because if it is working, it will wait until the program QBWC ends before the script continues.  Add some "litterals" inside ConsoleWrite so you know which statement has been executed.

ps. Sorry to say, but if you do not want to use ConsoleWrite as I asked you, I am afraid I will not be able to help you any more.

 

Sorry, I only attempt to write scripts when I have a specific need so my knowledge is lacking.

When I tried with ConsoleWrite I did not see any output. I will try modifying the script again as suggested and post it back.

Link to comment
Share on other sites

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...