Jump to content

Close action2, when action1 is running


Recommended Posts

Hello there,

I cant delete file in action1, if process exists. This is probably because the same file is used in action2.

If i Try only Action1, with start test.exe manually, its ok. ( without action2)

Can someone help me pls?

Local $Timer1, $Delay1 = 55000  
Local $Timer2, $Delay2 = 10000   


While 1
    If $Timer1 = '' Or TimerDiff($Timer1) > $Delay1 Then
        $Timer1 = TimerInit()
        Action1()
    EndIf
    If $Timer2 = '' Or TimerDiff($Timer2) > $Delay2 Then
        $Timer2 = TimerInit()
        Action2()
    EndIf
; Briefly pause to avoid excessive CPU usage
    Sleep(100)
WEnd

Func Action1()

   If ProcessExists("test.exe") Then ; Check if the process is running.
      ProcessClose("test.exe")                              ; - this is ok
      FileDelete("C:\ProgramData\test\test.exe")      ; - here is my problem

     

   Else

   EndIf



EndFunc

Func Action2()

$idleTimer = _Timer_GetIdleTime()

   If $idleTimer > 8000 And Not ProcessExists("test.exe") Then  
      Run("C:\ProgramData\test\test.exe
   ElseIf $idleTimer < 500 Then
      ProcessClose("test.exe")
   EndIf


EndFunc

 

Link to post
Share on other sites

Like this?

#include <Timers.au3>

Global $Timer1, $Delay1 = 55000
Global $Timer2, $Delay2 = 10000

Global Const $AppName = "test.exe"
Global Const $AppPath = "C:\ProgramData\test\" & $AppName

Global $CurrenAction = 0 ; Current Task Run

While 1
    If $Timer1 = '' Or TimerDiff($Timer1) > $Delay1 Then
        $Timer1 = TimerInit()
        Action1()
    EndIf
    If $Timer2 = '' Or TimerDiff($Timer2) > $Delay2 Then
        $Timer2 = TimerInit()
        Action2()
    EndIf
    ; Briefly pause to avoid excessive CPU usage
    Sleep(100)
WEnd

Func Action1()
    If $CurrenAction <> 0 Then Return
    $CurrenAction = 1 ; START
    If ProcessExists($AppName) Then ; Check if the process is running.
        ProcessClose($AppName)                            ; - this is ok
        FileDelete($AppPath)    ; - here is my problem
    Else

    EndIf
    $CurrenAction = 0 ; DONE
EndFunc   ;==>Action1

Func Action2()
    If $CurrenAction <> 0 Then Return
    $CurrenAction = 2 ; START
    $idleTimer = _Timer_GetIdleTime()
    If $idleTimer > 8000 And Not ProcessExists($AppName) Then
        Run($AppPath)
    ElseIf $idleTimer < 500 Then
        ProcessClose($AppName)
    EndIf
    $CurrenAction = 0 ; DONE
EndFunc   ;==>Action2

 

Regards,
 

Link to post
Share on other sites

Hi VIP,thank you very much, but its the same.I try add other file to delete, and this one was deleted..

#include <Timers.au3>

Global $Timer1, $Delay1 = 55000
Global $Timer2, $Delay2 = 10000

Global Const $AppName = "test.exe"
Global Const $AppPath = "C:\ProgramData\test\" & $AppName

Global $CurrenAction = 0 ; Current Task Run

While 1
    If $Timer1 = '' Or TimerDiff($Timer1) > $Delay1 Then
        $Timer1 = TimerInit()
        Action1()
    EndIf
    If $Timer2 = '' Or TimerDiff($Timer2) > $Delay2 Then
        $Timer2 = TimerInit()
        Action2()
    EndIf
    ; Briefly pause to avoid excessive CPU usage
    Sleep(100)
WEnd

Func Action1()
    If $CurrenAction <> 0 Then Return
    $CurrenAction = 1 ; START
    If ProcessExists($AppName) Then ; Check if the process is running.
        ProcessClose($AppName)                            ; - this is ok
        FileDelete(C:\ProgramData\test\testfile.txt)            ; - this one was deleted
        FileDelete($AppPath)    ; - here is my problem
    Else

    EndIf
    $CurrenAction = 0 ; DONE
EndFunc   ;==>Action1

Func Action2()
    If $CurrenAction <> 0 Then Return
    $CurrenAction = 2 ; START
    $idleTimer = _Timer_GetIdleTime()
    If $idleTimer > 8000 And Not ProcessExists($AppName) Then
        Run($AppPath)
    ElseIf $idleTimer < 500 Then
        ProcessClose($AppName)
    EndIf
    $CurrenAction = 0 ; DONE
EndFunc   ;==>Action2

 

Link to post
Share on other sites

@maklin If the process is in use. Maybe your computer is too slow. 

Try to add a wait in your function Action1 ()

Func Action1()
    If $CurrenAction <> 0 Then Return
    $CurrenAction = 1 ; START
    If ProcessExists($AppName) Then ; Check if the process is running.
        ProcessClose($AppName)                            ; - this is ok
        Sleep (5000) ; you reduce the time after the first try if it work.
        FileDelete($AppPath)    ; - here is my problem
    Else

    EndIf
    $CurrenAction = 0 ; DONE
EndFunc   ;==>Action1

 

My video tutorials : ( In construction )  || My Discord : https://discord.gg/S9AnwHw

How to Ask Help ||  UIAutomation From Junkew || WebDriver From Danp2 || And Water's UDFs in the Quote

Spoiler

 Water's UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Link to post
Share on other sites
12 minutes ago, maklin said:

@caramen, @VIP THX a lot, now its working good.I will test it more, but i think its OK.

 

 

You got sometime further actions when you close an application. 

Temp cleanning, services closing... what ever... It can long sometime more than a minute... 

My video tutorials : ( In construction )  || My Discord : https://discord.gg/S9AnwHw

How to Ask Help ||  UIAutomation From Junkew || WebDriver From Danp2 || And Water's UDFs in the Quote

Spoiler

 Water's UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

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

    No registered users viewing this page.

×
×
  • Create New...