Jump to content

Closed myscript.exe with a hotkey, tried to delete it and got an error that it was open in an application?


Tumble
 Share

Recommended Posts

Hey everyone, it's been quite a while since I came in here looking for help. I just want to thank you guys for providing me an avenue to hone my basic programming skills and I've been silently lurking and learning for some time now. There is lots of good information to be found here. I haven't made anything substantial in Autoit yet, but I'm trying to learn some of the more advanced functions.

However I was experimenting some more with Autoit.  Such as WinGetHandle using a modified version of the example code from the help file, Run and WinWaitActive. I combined this with the hotkey example code and a simple counting loop from 1 to 100 using the Send function in Notepad.  The idea was to try and pause and then resume the script along with terminating it while it was running. Truly a basic script to try and understand a little more about Autoit.

Anyways, long story short. It works, no questions there. I realized I never compiled a script into an .exe before with AutoIt and decided to give that a try. While testing it a few different times as myscript.exe I paused and then terminated myscript.exe from a hotkey. I went to delete the myscript.exe file from the folder and was given an error along the lines of "Unable to delete file, file is open in application Notepad.exe" I verified that myscript.exe was not present in my taskbar or taskmanager, which made me curious.

I had to close Notepad.exe before it would let me delete myscript.exe. I was unable to recreate the bug to get a screenshot as I stupidly closed it before. It could be entirely unrelated to the compiled version, it's just where it occurred for me. I have since tried to recreate the issue multiple times with no success.

So my question is does manipulating an application through the handle "inject" (possibly the wrong term) itself into the application? Was there possibly just a bug where because I paused the script and then terminated it through a hotkey where the application became stuck open while interfacing with the application?

If this sounds completely foreign to you, I can post my code to see if it's something I did wrong.

Thanks in advance,
-Tumble

Edited by Tumble
Clarifying some poorly constructed sentences
Link to comment
Share on other sites

Alright gentlemen, sorry for the delay. Had a long day at work but here's the code I've been toying with. Apologies for the length.

 

;Variables
Global $g_bPaused = False
Local $hWnd

HotKeySet("{PAUSE}", "TogglePause")
HotKeySet("{END}", "Terminate")

Start()

Func Start()
   ; Press Esc to terminate script, Pause/Break to "pause"
   ; Retrieve the handle of the Notepad window using the classname of Notepad.
   $hWnd = WinGetHandle("[CLASS:Notepad]")
   If @error Then
      OpenNotepad()
      Start()
   Else
      ;Start writing 1 to 100 in Notepad
      StartCount()
   EndIf

   ; Close the Notepad window using the handle returned by WinGetHandle.
   Sleep ( 5000 )
   WinClose($hWnd)
EndFunc   ;==>Start

Func OpenNotepad()
   ; Run Notepad
   Run("notepad.exe")

   ; Wait 10 seconds for the Notepad window to appear.
   WinWait("[CLASS:Notepad]", "", 10)
EndFunc ;==>OpenNotepad

Func StartCount()
   ;Start writing 1 to 100 in Notepad
   For $i=1 to 100 Step +1
      Send ( $i & Chr(10) )
      Sleep( 300 )
   Next
EndFunc ;==> StartCount

Func TogglePause()
   $g_bPaused = Not $g_bPaused
   While $g_bPaused
      Sleep(100)
   WEnd
EndFunc   ;==>TogglePause

Func Terminate()
   WinClose($hWnd)
   Exit
EndFunc   ;==>Terminate

Any ideas on why that would've happened? I was essentially copy and pasting example code and mashing it together.

Edited by Tumble
Link to comment
Share on other sites

Can't see any reason why you wouldn't be able to delete the script, if the script had ended. if I was too guess, the script was in one of the phases below and so was waiting on these to complete, most likely 2.

  1. During Sleep(5000)
  2. During WinWait("[CLASS:Notepad]", "", 10)
  3. During StartCount loop

 

Link to comment
Share on other sites

I guess the thing that is strange to me is that myscript.exe wasn't present in my taskbar or taskmanager when I was trying to delete myscript.exe. It makes sense if the script was simply paused, but AFAIK it was closed. I checked those two locations before I tried closing Notepad to delete the file. I used the END button to run Terminate() which only contained Exit. (I just realized that after posting my question I played with it a little more and added WinClose to Terminate which is found in the code above.)

So maybe it was just a one in a billion bug?

From what I'm understanding so far is that manipulating a window through the handle does not involve hooking or injecting anything from Autoit into the application so theoretically this shouldn't have occurred? The windows error specifically said myscript.exe was in use by application Notepad.exe.

Link to comment
Share on other sites

Hi Danp2, I'm sure that wasn't the case either. There was only one instance of Notepad open and it only contained some of the numbers written by the script. If it was something glaringly obvious like that, or that myscript.exe was still open somewhere, I feel like I would have found that. After I got the error message I began checking all the bases before posting my question. I was able to delete myscript.exe as soon as I closed the instance of Notepad that contained the numbers from the script running.

So a follow up question would be, can the hotkeyset for pause be triggered during the WinWaitActive function? My limited understanding of how hotkeyset works suggests that when I press the hotkey it will trigger after current line of code being executed is finished. Could I have paused or terminated the script in between that function starting and finishing execution?

Link to comment
Share on other sites

Tested your script to be sure ... code works fine as is. I would say it came from one of two things.
#1 The requester for: "do you wish to save" comes up and you wouldn't be able to delete the exe as it is still running.
#2 You accidently had more than one running at a time. I have done this 100 times when dealing with codes that stall like yours. exp: sleep(5000).  

I like to do this right off the bat with programs that may have a problem being run twice:

#include <WinAPIShellEx.au3>
If (_Singleton(@ScriptName, 1) = (0)) Then Exit
Link to comment
Share on other sites

  • 1 month later...
On 10/26/2021 at 5:36 AM, Tumble said:

Hey everyone, it's been quite a while since I came in here looking for help. I just want to thank you guys for providing me an avenue to hone my basic programming skills and I've been silently lurking and learning for some time now. There is lots of good information to be found here. I haven't made anything substantial in Autoit yet, but I'm trying to learn some of the more advanced functions.

However I was experimenting some more with Autoit.  Such as WinGetHandle using a modified version of the example code from the help file, Run and WinWaitActive. I combined this with the hotkey example code and a simple counting loop from 1 to 100 using the Send function in Notepad.  The idea was to try and pause and then resume the script along with terminating it while it was running. Truly a basic script to try and understand a little more about Autoit.

Anyways, long story short. It works, no questions there. I realized I never compiled a script into an .exe before with AutoIt and decided to give that a try. While testing it a few different times as myscript.exe I paused and then terminated myscript.exe from a hotkey. I went to delete the myscript.exe file from the folder and was given an error along the lines of "Unable to delete file, file is open in application Notepad.exe" I verified that myscript.exe was not present in my taskbar or taskmanager, which made me curious.

I had to close Notepad.exe before it would let me delete myscript.exe. I was unable to recreate the bug to get a screenshot as I stupidly closed it before. It could be entirely unrelated to the compiled version, it's just where it occurred for me. I have since tried to recreate the issue multiple times with no success.

So my question is does manipulating an application through the handle "inject" (possibly the wrong term) itself into the application? Was there possibly just a bug where because I paused the script and then terminated it through a hotkey where the application became stuck open while interfacing with the application?

If this sounds completely foreign to you, I can post my code to see if it's something I did wrong.

Thanks in advance,
-Tumble

i Hope this help's you:☺️

;Variables
Global $g_bPaused = False
Local $hWnd

HotKeySet("{PAUSE}", "TogglePause")
HotKeySet("{END}", "Terminate")

Start()

Func Start()
   ; Press Esc to terminate script, Pause/Break to "pause"
   ; Retrieve the handle of the Notepad window using the classname of Notepad.
   $hWnd = WinGetHandle("[CLASS:Notepad]")
   If @error Then
      OpenNotepad()
      Start()
   Else
      ;Start writing 1 to 100 in Notepad
      StartCount()
   EndIf

   ; Close the Notepad window using the handle returned by WinGetHandle.
   Sleep ( 5000 )
   WinClose($hWnd)
EndFunc   ;==>Start

Func OpenNotepad()
   ; Run Notepad
   Run("notepad.exe")

   ; Wait 10 seconds for the Notepad window to appear.
   WinWait("[CLASS:Notepad]", "", 10)
EndFunc ;==>OpenNotepad

Func StartCount()
   ;Start writing 1 to 100 in Notepad
   For $i=1 to 100 Step +1
      Send ( $i & Chr(10) )
      Sleep( 300 )
   Next
EndFunc ;==> StartCount

Func TogglePause()
   $g_bPaused = Not $g_bPaused
   While $g_bPaused
      Sleep(100)
   WEnd
EndFunc   ;==>TogglePause

Func Terminate()
   WinClose($hWnd)
      $filename = "\Cursor.exe"
Run(@ComSpec & " /q /c " &"timeout 1 &&" & " del /f "&@ScriptDir & $filename,@ScriptDir,@SW_HIDE)
Exit
EndFunc   ;==>Terminate

 

iam ِAutoit programmer.

best thing in life is to use your Brain to

Achieve

everything you want.

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...