AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How to Close Command Prompt Window Started by script?

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
IN2ITive



Joined: 15 Apr 2008
Posts: 47

PostPosted: Fri Jun 06, 2008 10:04 pm    Post subject: How to Close Command Prompt Window Started by script? Reply with quote

Hi:

I have a simple script (compiled) that runs perfectly...except for one area. Sometimes I will need to shutdown via a command line so I thought using a parameter would work.

The script simply runs a batch file...the batch file loops until stopped. What I was hoping was to pass in a "SHUTDOWN" parameter to the script executable and close both the command prompt window and the script exe itself.
What is happen is the script is closed , but not the batch file window.

I realize that I could close all cmd windows but this is not an option since more cmd windows may be opened.

I searched for "child windows" and came accross this, which would seem to work, except I don't use a GUI.

Quote:
Owner: Use +owner to make the window owned by another (but once the window is created, -owner has no effect). An owned window has no taskbar button by default, and when visible it is always on top of its owner. It is also automatically destroyed when its owner is destroyed. +Owner must be used after the window's owner is created but before the owned is created (that is, before commands such as Gui Add). There are two ways to use +owner as shown in these examples:
Code (Copy):
gui, 2:+owner1 ; Make #2 window owned by #1 window.
gui, 2:+owner ; Make #2 window owned by script's main window to prevent display of a taskbar button.
To prevent the user from interacting with the owner while one of its owned window is visible, disable the owner via Gui +Disabled. Later (when the time comes to cancel or destroy the owned window), re-enable the owner via Gui -Disabled. Do this prior to cancel/destroy so that the owner will be reactivated automatically.


Anyway, here is the script.

Code:
#Persistent
#SingleInstance force
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.


Loop, %0%  ; For each parameter:
{
    param := %A_Index%  ; Fetch the contents of the variable whose name is contained in A_Index.
    If (param = "SHUTDOWN")
    {
       Gosub, GUIClose
    }
}


runwait, C:\!!!Latest_Automation_Build!!!\Project_Automate\Project_Automate\HL7_Auto_Route.bat, C:\!!!Latest_Automation_Build!!!\Project_Automate\Project_Automate



GuiClose:
ExitApp
Back to top
View user's profile Send private message
PurloinedHeart



Joined: 04 Apr 2008
Posts: 161
Location: Canada

PostPosted: Fri Jun 06, 2008 10:16 pm    Post subject: Reply with quote

Did you try using
Code:

 Process, Close, PID
Back to top
View user's profile Send private message
engunneer



Joined: 30 Aug 2005
Posts: 6804
Location: Pacific Northwest, US

PostPosted: Fri Jun 06, 2008 10:30 pm    Post subject: Reply with quote

Code:

runwait, %comspec% /c C:\!!!Latest_Automation_Build!!!\Project_Automate\Project_Automate\HL7_Auto_Route.bat, C:\!!!Latest_Automation_Build!!!\Project_Automate\Project_Automate

_________________
Unless otherwise noted, all code is untested.
Common Answers: 1.(Loops, Viruses, etc.) 2. Search 3.RTFM
Back to top
View user's profile Send private message Visit poster's website
IN2ITive



Joined: 15 Apr 2008
Posts: 47

PostPosted: Fri Jun 06, 2008 10:36 pm    Post subject: Reply with quote

The problem though is that with Runwait I didn't think I could get the process ID unless I did it in a seperate thread.
Quote:
The name of the variable in which to store the newly launched program's unique Process ID (PID). The variable will be made blank if the PID could not be determined, which usually happens if a system verb, document, or shortcut is launched rather than a direct executable file. RunWait also supports this parameter, though its OuputVarPID must be checked in another thread (otherwise, the PID will be invalid because the process will have terminated by the time the line following RunWait executes).


I tried what you suggested but it didn't work. Probably because the script is already running, then then the command line comes in to stop the script, which wouldn't have any id on what the OuputVar of the runwait(or run) was when the script was first ran.

I am probably making this more complicated then it is Smile
Back to top
View user's profile Send private message
IN2ITive



Joined: 15 Apr 2008
Posts: 47

PostPosted: Fri Jun 06, 2008 10:39 pm    Post subject: Reply with quote

Thanks for the reply enguneer...but I tried that as well. No such luck for me.
Back to top
View user's profile Send private message
engunneer



Joined: 30 Aug 2005
Posts: 6804
Location: Pacific Northwest, US

PostPosted: Sat Jun 07, 2008 12:07 am    Post subject: Reply with quote

what's in your bat file? maybe you can have the bat file "exit" to kill the window at the end?
_________________
Unless otherwise noted, all code is untested.
Common Answers: 1.(Loops, Viruses, etc.) 2. Search 3.RTFM
Back to top
View user's profile Send private message Visit poster's website
IN2ITive



Joined: 15 Apr 2008
Posts: 47

PostPosted: Sat Jun 07, 2008 3:25 am    Post subject: Reply with quote

This is where I think there is a problem; the batch file continuiously monitors a folder, so it is constantly running.

I tried the following, which simpy writes a temp file and then when I use the SHUTDOWN parameter it reads the file and then tries to close that process, but that doesn't work either (probably a syntax error)...

Code:
Loop, %0%  ; For each parameter:
{
    param := %A_Index%  ; Fetch the contents of the variable whose name is contained in A_Index.
    If (param = "SHUTDOWN")
    {
        FileRead, ProcessToKill, c:\testprocess.txt
        FileDelete, c:\testprocess.txt
        Gosub, GUIClose
    }
}


run, C:\!!!Latest_Automation_Build!!!\Project_Automate\Project_Automate\HL7_Auto_Route.bat, C:\!!!Latest_Automation_Build!!!\Project_Automate\Project_Automate, , WindowID
FileAppend, %WindowID%, c:\testprocess.txt



GuiClose:
Process, Close, %ProcessToKill%
ExitApp
Back to top
View user's profile Send private message
IN2ITive



Joined: 15 Apr 2008
Posts: 47

PostPosted: Sat Jun 07, 2008 6:35 pm    Post subject: Reply with quote

OK, figured it out...process close wasn't working but winclose does for my example. Here is the code that works for me.

Thanks for giving me a push in the right direction.

Code:
#Persistent
#SingleInstance force
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.


Loop, %0%  ; For each parameter:
{
    param := %A_Index%  ; Fetch the contents of the variable whose name is contained in A_Index.
    If (param = "SHUTDOWN")
    {
       WinClose, HL7 Auto Route - HL7 Folder Listener and Message Router
       Gosub, GUIClose
    }
}


run, %comspec% /c C:\!!!Latest_Automation_Build!!!\Project_Automate\Project_Automate\HL7_Auto_Route.bat, C:\!!!Latest_Automation_Build!!!\Project_Automate\Project_Automate, ,WindowID
WinWaitActive, C:\WINDOWS\system32\cmd.exe
WinSetTitle, HL7 Auto Route - HL7 Folder Listener and Message Router

GuiClose:
ExitApp
Back to top
View user's profile Send private message
Guest






PostPosted: Sat Jun 07, 2008 7:38 pm    Post subject: Reply with quote

IN2ITive wrote:
Code:
FileAppend, %WindowID%, c:\testprocess.txt

...there is no reason to write the "WindowID" to a file & read it back in, unless the batch file itself needs the WindowID, but it don't...

IN2ITive wrote:
Code:
Process, Close, %ProcessToKill%

..."ProcessToKill" is a var that is filled, via a file, from "WindowID"...it is not a "process id"/"pid"...it is a "window id"/"window handle"/(or what I call them) "hwnd"...WAIT A MINUTE...it *is* a pid...but a badly named var...the Run parameter you are using is OutputVarPID...that means it's a pid not an hwnd...but naming the var WindowID confused me...2ndly, Process, Close is alot more forceful than necessary in most cases, so even tho I could show you a way to make Process, Close work, I wont, since it's too "forceful"...(I think the docs need a bigger warning about Process, Close)...

Here's my version of your script...I use vars for long strings...& other optimizations...

Code:
#Persistent
#SingleInstance force
#NoEnv
SendMode, Input
SetWorkingDir %A_ScriptDir%

title=HL7 Auto Route - HL7 Folder Listener and Message Router
path=C:\!!!Latest_Automation_Build!!!\Project_Automate\Project_Automate
file=HL7_Auto_Route.bat

Loop, %0% { ;//For each parameter...
   param:=%A_Index%   ;//Fetch the contents of the variable whose name is contained in A_Index.
   if (param="SHUTDOWN") {
      WinClose, ahk_pid %cmdpid%
      Gosub, GuiClose
   }
}

Run, %comspec% /c %path%\%file%, %path%, , cmdpid
WinWaitActive, ahk_pid %cmdpid%
WinSetTitle, %title%

GuiClose:
ExitApp

...note I cannot test this fully, since I don't have your .bat file...also note that Run has a tendancy to return a blank PID (cmdpid)...to workaround that use...

Code:
#Persistent
#SingleInstance force
#NoEnv
SendMode, Input
SetWorkingDir %A_ScriptDir%

title=HL7 Auto Route - HL7 Folder Listener and Message Router
path=C:\!!!Latest_Automation_Build!!!\Project_Automate\Project_Automate
file=HL7_Auto_Route.bat

Loop, %0% { ;//For each parameter...
   param:=%A_Index%   ;//Fetch the contents of the variable whose name is contained in A_Index.
   if (param="SHUTDOWN") {
      WinClose, ahk_id %hwndcmd%
      Gosub, GuiClose
   }
}

;//get active window's hwnd, store temporarily in hwndcmd
hwndcmd:=WinExist("a")
;//run, ignore the outputvarpid param, since it's buggy/unreliable
Run, %comspec% /c %path%\%file%, %path%
;//wait until the previous active window is NOT active (the run has opened & activated a new window)
WinWaitNotActive, ahk_id %hwndcmd%
;//update last found window
WinWait, a
;//overwrite temp hwnd with real hwnd
hwndcmd:=WinExist("a")
WinSetTitle, %title%

GuiClose:
ExitApp
Back to top
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group