Jump to content

ProcessClose


Nemcija
 Share

Recommended Posts

I have got a problem here with ProcessClose.

I'm writing a program which starts an other program, works with it an closes it again, if the process is finished.

The code looks like this:

;~ [...]
#include <Process.au3>
;~ [...]

Func _runcheckroutinefunction($_pid)
;~ isn't needed to post it here, because it is too long and not the problem
;~ works absolutely correctly ;)
EndFunc

$pid = run("program.exe")
;~ [...]
While _runcheckroutinefunction($pid) = 1
     ProcessClose($pid)
     Sleep(100)
     ;~ And this loop will only end if i close the process in taskmanager
WEnd

Please don't tell me that i should use ProcessClose("program.exe") (of course it works !!) because there are more of this, so i have to close exactly this PID.

But it dosent work :)

Thank you for helping.

Greetings, Nemcija.

[url=www.vergessene-welt.de][/url]

Link to comment
Share on other sites

run("program.exe")
$pid = ProcessExists("program.exe")

While _runcheckroutinefunction($pid) = 1
     ProcessClose($pid)
     Sleep(100)
     ;~ And this loop will only end if i close the process in taskmanager
WEnd

though it had to be like this...

Neo

[center][font="Arial"]--- The Neo and Only --- [/font][font="Arial"]--Projects---[/font]Image to Text converterText to ASCII converter[/center]

Link to comment
Share on other sites

I doubt that would work the way it is, since ProcessExists returns the pid which won't be 1.

$app = 'program.exe'

While ProcessExists($app)
     ProcessClose($app)
     Sleep(100)
     ;~ And this loop will only end if i close the process in taskmanager
WEnd

edit - forgot something. And that should give you an idea of how maybe to work it in to your function. :)

Edited by xcal
Link to comment
Share on other sites

Separate your trouble shooting from your script.

How does this work for you?

<dumb code removed>

Edit: my bad - according to the help file under ProcessClose

Return ValueNone. (Returns 1 regardless of success/failure.)

Edited by herewasplato

[size="1"][font="Arial"].[u].[/u][/font][/size]

Link to comment
Share on other sites

  • Moderators

@NeoFoX - The Run() function returns a PID as well, so the way he is doing it is fine.

---

@xcal - It doesn't have to be "1", the expression just has to evaluate to true for the loop to continue. With that in mind any string or positive integer will evaluate as true.

Edited by big_daddy
Link to comment
Share on other sites

@Neo: I have got the PID.

Run returns the PID!

@xcal: It is the same as ProcessClose("program.exe") please read my post first, but thanks.

@big_daddy: Is "taskkill" a windows function?

@herewasplato: doesn't work that is exactly what i'm doing up there, but it doesn't work.

Thanks,

Greetings Nemcija.

Edited by Nemcija

[url=www.vergessene-welt.de][/url]

Link to comment
Share on other sites

Does WinClose or WinKill do anything for you?

No because every "program.exe" have the same title.

So any random process would be closed.

But i think that ShellExecuteWait("taskkill", "/F /PID " & $PID) will do it. My only problem at the moment is, that "taskkill" has to be available on any computer.

Edited by Nemcija

[url=www.vergessene-welt.de][/url]

Link to comment
Share on other sites

i can't change it.

Because is there anywhere another window with the same title as the title of "program.exe" it be replaced.

p.e.:

anotherprogram.exe Title: "bla"

program.exe Title: "bla"

WinSetTitle("bla","","bla bla") -> anotherprogram.exe Title: "bla bla"

do you understand?

[url=www.vergessene-welt.de][/url]

Link to comment
Share on other sites

Ok then i will do it like this ..

func _CloseAPP($_closeapp_appname="",$_closeapp_pid=-1)
    If FileExists(@WindowsDir&"\system32\taskkill.exe") Then
        if $_closeapp_pid > 0 and procsreq($_closeapp_appname,"run",$_closeapp_pid) <> 0 Then ShellExecuteWait("taskkill", "/F /PID " & $_closeapp_pid,"","",@SW_HIDE)
    EndIf
    if $_closeapp_pid > 0 and procsreq($_closeapp_appname,"run",$_closeapp_pid) <> 0 Then ProcessClose($_closeapp_pid)
    if $_closeapp_appname <> "" and procsreq($_closeapp_appname,"run",$_closeapp_pid) <> 0 Then ProcessClose($_closeapp_appname)
    If $_closeapp_pid > 0 and procsreq($_closeapp_appname,"run",$_closeapp_pid) <> 0 Then WinKill(procsreq($_closeapp_appname,"title",$_closeapp_pid))
EndFunc

procsreq is the real name of _runcheckroutinefunction() :)

it returns much more things than only that the program runs or not

Thank you big daddy an the others, too.

Greetings Nemcija.

[url=www.vergessene-welt.de][/url]

Link to comment
Share on other sites

i can't change it.

Because is there anywhere another window with the same title as the title of "program.exe" it be replaced.

p.e.:

anotherprogram.exe Title: "bla"

program.exe Title: "bla"

WinSetTitle("bla","","bla bla") -> anotherprogram.exe Title: "bla bla"

do you understand?

It looks like you have your work around... but I think that there is a way to know which window is associated with a given PID. You would only change the window title associated with the process that the script started.

...later...

[size="1"][font="Arial"].[u].[/u][/font][/size]

Link to comment
Share on other sites

Func _GetWHNDLFromPID($PID)
Local $wHndl="", $WCCount=0
Do
Sleep(50)
if $WCCount > 400 Then Return -1
Local $WList=WinList()
for $WLCount= 1 to $WList[0][0]
if WinGetProcess($WList[$WLCount][1])=$PID Then
$wHndl=$WList[$WLCount][1]
ExitLoop
EndIf
Next
$WCCount += 1
Until not($wHndl="")
Return $wHndl
EndFunc

using this function, you can obtain the Handle of a window Associated with a specific PID. Once obtained, you can do a simple winclose($wHndl) to close only that window. One thing to note; I wrote this particular function intending that it would only need to find the Hndl of one window. If the process has more than one window associated with it, you may have to either re-work the function so that it will return multiple handles, or call the function more than once.

Edit: Minor correction to code

Edited by improbability_paradox
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...