Sign in to follow this  
Followers 0
MrCreatoR

ProcessExists on the same process

12 posts in this topic

#1 ·  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

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Share this post


Link to post
Share on other sites



#2 ·  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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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?


 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Share this post


Link to post
Share on other sites

#5 ·  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

Visit the SciTE4AutoIt3 Download page for the latest versions  - Beta files                                How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

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

I know, it's not <_<

Do you see two instances of Task Manager?

Sometimes i do, but for few seconds.

Any suggestion on how to acomplish my task?


 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.


 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

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.


 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Share this post


Link to post
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
Sign in to follow this  
Followers 0