leo8888 Posted December 2, 2020 Share Posted December 2, 2020 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. expandcollapse popup#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 More sharing options...
Nine Posted December 2, 2020 Share Posted December 2, 2020 Maybe adding some error handling could help you understand what is the problem ? “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
Dan_555 Posted December 2, 2020 Share Posted December 2, 2020 (edited) removed Edited December 2, 2020 by Dan_555 Pasted the answer in a wrong thread ! Some of my script sourcecode Link to comment Share on other sites More sharing options...
leo8888 Posted December 2, 2020 Author Share Posted December 2, 2020 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 More sharing options...
Nine Posted December 2, 2020 Share Posted December 2, 2020 Use consoleWrite after each statement (check for @error and return code) “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
Nine Posted December 2, 2020 Share Posted December 2, 2020 @Dan_555 wrong thread Dan_555 1 “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
leo8888 Posted December 2, 2020 Author Share Posted December 2, 2020 Thank you for the tip! I will give it a try. Link to comment Share on other sites More sharing options...
Nine Posted December 2, 2020 Share Posted December 2, 2020 Example : #include <Constants.au3> Opt ("MustDeclareVars", 1) Local $iPID = Run ("Notepad.exe") ConsoleWrite ($iPID & "/" & @error & @CRLF) Local $hWnd = WinWaitActive ("[CLASS:Notepad]") ConsoleWrite ($hWnd & "/" & @error & @CRLF) Local $iRet = RunWait ("TaskKill /IM notepad.exe /F") ConsoleWrite ($iRet & "/" & @error & @CRLF) “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
JockoDundee Posted December 2, 2020 Share Posted December 2, 2020 (edited) 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 December 2, 2020 by JockoDundee Changed waitproccessclose to ProcessWaitClose Code hard, but don’t hard code... Link to comment Share on other sites More sharing options...
Nine Posted December 2, 2020 Share Posted December 2, 2020 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 “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
leo8888 Posted December 2, 2020 Author Share Posted December 2, 2020 Thank you all for your suggestions. I have tried multiple combinations of ShellExecuteWait and RunWait for both commands with the same result. I will give your other suggestions a try and post back. Link to comment Share on other sites More sharing options...
JockoDundee Posted December 2, 2020 Share Posted December 2, 2020 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 More sharing options...
Nine Posted December 2, 2020 Share Posted December 2, 2020 (edited) 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 December 2, 2020 by Nine “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
leo8888 Posted December 2, 2020 Author Share Posted December 2, 2020 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. expandcollapse popup#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 More sharing options...
pseakins Posted December 2, 2020 Share Posted December 2, 2020 (edited) <snip> Edited December 2, 2020 by pseakins irrelevant post Phil Seakins Link to comment Share on other sites More sharing options...
Nine Posted December 2, 2020 Share Posted December 2, 2020 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. “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
JockoDundee Posted December 2, 2020 Share Posted December 2, 2020 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 More sharing options...
leo8888 Posted December 3, 2020 Author Share Posted December 3, 2020 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 More sharing options...
Nine Posted December 3, 2020 Share Posted December 3, 2020 Just now, leo8888 said: When I tried with ConsoleWrite I did not see any output You must use Scite editor to see ConsoleWrite result. Execute script by pressing F5. “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
leo8888 Posted December 3, 2020 Author Share Posted December 3, 2020 3 minutes ago, Nine said: You must use Scite editor to see ConsoleWrite result. Execute script by pressing F5. Thank you, I am trying to modify the script now so I can try again. I appreciate your help. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now