Jump to content

Recommended Posts

Posted (edited)

Hi all,

I would like to make my own _ProcessWaitClose function, i need it because i need to call some function while the process is exists..

Here is example:

$Pid = Run("taskmgr.exe")

_ProcessWaitClose($Pid, 0, "My_Func", $Pid)

Func _ProcessWaitClose($ProcName, $TimeOut=0, $CallFunc="", $Param="")
    Local $iTimer = TimerInit()
    While ProcessExists($ProcName) > 0
        If $TimeOut > 0 And TimerDiff($iTimer) >= $TimeOut Then Return 0
        If $CallFunc <> "" Then Call($CallFunc, $Param)
        Sleep(100)
    WEnd
    Return 1
EndFunc

Func My_Func($ProcID)
    ConsoleWrite($ProcID & @LF)
EndFunc

If the taskmgr.exe process not exists yet, then all ok, the function wait until it closed and call other function while it wating, but if the process taskmgr.exe was exists already (when we start the script), then the script exits without waiting <_<

I guesing that the problem is with ProcessExists, it's return incorrect PID.

Someone have an idea on how to solve it? (without specify a process name, i need it only by PID).

Edited by MsCreatoR

 

  Reveal hidden contents

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

  • Moderators
Posted (edited)

Could try this at the beginning of the function:

If UBound(ProcessList($ProcName)) - 1 = 1 Then Return SetError(1, 0, 0)

Edit:

Think I'm reading what you want wrong.

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Posted

Maybe this.

$theProcess = "taskmgr.exe"
$Pid = ProcessExists($procName)

If NOT $Pid
    $Pid = Run($theProcess)
EndIf

_ProcessWaitClose($Pid, 0, "My_Func", $Pid)

Func _ProcessWaitClose($ProcName, $TimeOut=0, $CallFunc="", $Param="")
    Local $iTimer = TimerInit()
    While ProcessExists($ProcName)
        If $TimeOut > 0 And TimerDiff($iTimer) >= $TimeOut Then Return 0
        If $CallFunc <> "" Then Call($CallFunc, $Param)
        Sleep(100)
    WEnd
    Return 1
EndFunc

Func My_Func($ProcID)
    ConsoleWrite($ProcID & @LF)
EndFunc
Posted

  Quote

Maybe this

Thanks, but i need to run again even if the process exists.

I think there is a bug in ProcessWaitClose (built-in function)...

If Not ProcessExists("taskmgr.exe") Then Run("taskmgr.exe")
$Pid = Run("taskmgr.exe")

$Ret = ProcessWaitClose($Pid)
ConsoleWrite(@error & @LF & $Ret & @LF & ProcessExists($Pid))

The process ID in $Pid is the one that was executed last, so why this process not exits?

 

  Reveal hidden contents

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

  • Developers
Posted (edited)

I don't think the last Taskmgr.exe stays running when its already existing.

perform these steps on the command prompt:

>tasklist /FI "imagename eq taskmgr.exe"

>Taskmgr

>tasklist /FI "imagename eq taskmgr.exe"

You will see that the same pid stays running ..

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Posted (edited)

  MsCreatoR said:

Thanks, but i need to run again even if the process exists.

I think there is a bug in ProcessWaitClose (built-in function)...

If Not ProcessExists("taskmgr.exe") Then Run("taskmgr.exe")
$Pid = Run("taskmgr.exe")

$Ret = ProcessWaitClose($Pid)
ConsoleWrite(@error & @LF & $Ret & @LF & ProcessExists($Pid))

The process ID in $Pid is the one that was executed last, so why this process not exits?

You shouldn't be able to open two instances of Task Manager. When you try to start the second, it sees the first already running and exits. So by the time you want to close it, it's already closed itself. Just run with the first two lines and nothing else. Do you see two instances of Task Manager?

<_<

Edit: Beat out by the Jos-bot! Rats!

Edited by PsaltyDS
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Posted

  Quote

I don't think the last Taskmgr.exe stays running when its already existing

I know, it's not <_<

  Quote

Do you see two instances of Task Manager?

Sometimes i do, but for few seconds.

Any suggestion on how to acomplish my task?

 

  Reveal hidden contents

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Posted

  MsCreatoR said:

Any suggestion on how to acomplish my task?

What was the task? Get two instances of Task Manager to stay open at the same time? It won't do that, because it's internally coded not to do that. So what is it you want to do?

<_<

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Posted

Perhaps it's a case of wanting to to close a certain specific process when more than one process exists with the same name?

So the examples would be (should be) something other than task manager. Notepad.exe perhaps?

I may be misunderstanding.

I had to do a similar thing a month ago.

Posted

  Quote

What was the task?

To write a proper function to wait untill process closes, and while waiting i need to call constantly certain function.

But it seems that this is not work with my example (it not necessary taskmgr.exe, but the same situation can arise with any other process), it work correct only if i use process name, not process id, wich is what i need actualy.

 

  Reveal hidden contents

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Posted (edited)

  MsCreatoR said:

To write a proper function to wait untill process closes, and while waiting i need to call constantly certain function.

But it seems that this is not work with my example (it not necessary taskmgr.exe, but the same situation can arise with any other process), it work correct only if i use process name, not process id, wich is what i need actualy.

OK, given that you don't want the function to actually close the process, just wait for it to close, it works fine for me. I made one change to see that the returned value was correct for success/timeout and it worked fine, even with Task Manager, provided Task Manager was not already running. So I changed the top to get the PID of an existing Task Manager or start a new one, but not try to have two:

If ProcessExists("taskmgr.exe") Then
    $Pid = ProcessExists("taskmgr.exe")
Else
    $Pid = Run("taskmgr.exe")
EndIf

$RET = _ProcessWaitClose($Pid, 3000, "My_Func", $Pid)
ConsoleWrite("Debug: $RET = " & $RET & @LF)

Func _ProcessWaitClose($ProcName, $TimeOut=0, $CallFunc="", $Param="")
    Local $iTimer = TimerInit()
    While ProcessExists($ProcName) > 0
        If $TimeOut > 0 And TimerDiff($iTimer) >= $TimeOut Then Return 0
        If $CallFunc <> "" Then Call($CallFunc, $Param)
        Sleep(100)
    WEnd
    Return 1
EndFunc

Func My_Func($ProcID)
    ConsoleWrite($ProcID & @LF)
EndFunc

This seems to work exactly as you wanted, writing the PID to the console until either timeout or the process is closed. What makes you think ProcessExists() doesn't work with your $Pid?

<_<

Edited by PsaltyDS
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Posted

  Quote

So I changed the top to get the PID of an existing Task Manager or start a new one

Thanks for trying to help me, i really appreciate it.

I thouth that it can be done from inside the UDF (manage the given pid), but probably i will have to change the concept of the function (something like _ProcessRunWaitClose()).

Thanks to all, i think the problem(?) is solved.

 

  Reveal hidden contents

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

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
  • Recently Browsing   0 members

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