Sign in to follow this  
Followers 0
PKG

How to terminate a running script from another script ?

6 posts in this topic

#1 ·  Posted (edited)

Hi.

I want to know that how can i terminate a running script from another script. I use ShellExecute() to run script and I want to stop it when i press "Stop" button.

;It is the 1st Script...

#include<GUIConstantsEx.au3>
Opt("GUIOnEventMode", 1)
Opt("MustDeclareVars", 1)
Global $PixFlag = False
Global $WinScrStart, $WinScrStop
FileInstall("ScrPix.au3", "", 1)
WinMain()
Func WinMain()
    GUICreate("WinApp-1.0", 550, 500)
    GUISetOnEvent($GUI_EVENT_CLOSE, "WinExit")
    $WinScrStart = GUICtrlCreateButton("&Start(Win)", 40, 420, 80, 30)
    GUICtrlSetOnEvent(-1, "PixCtrl")
    $WinScrStop = GUICtrlCreateButton("&Stop(Win)", 130, 420, 80, 30)
    GUICtrlSetOnEvent(-1, "PixCtrl")
    GUISetState()

    While 1
        Sleep(10)
        If $PixFlag = True Then
            FullPixSave()
        EndIf
    WEnd
    Return (0)
EndFunc   ;==>WinMain

Func WinExit()
    Exit 0
EndFunc   ;==>WinExit

Func PixCtrl()
    Switch @GUI_CtrlId
        Case $WinScrStart
            $PixFlag = True
        Case $WinScrStop
            $PixFlag = False
    EndSwitch
    Return
EndFunc   ;==>PixCtrl

Func FullPixSave()
    ShellExecute("ScrPix.au3")
    Return
EndFunc   ;==>FullPixSave
;It is the 2nd Script...

#include <ScreenCapture.au3>
WinPixStartSave()
Func WinPixStartSave()
    While 1
        Local $FileName
        $FileName = @WorkingDir & "\" & @HOUR & @MIN & @SEC & "WinScr.jpg"
        _ScreenCapture_CaptureWnd($FileName, WinGetHandle("", ""))
        Sleep(1000)
    WEnd
    Return
EndFunc   ;==>WinPixStartSave

When i run the second script, It uses 100% CPU. How can i Fix it ?

Thanks in Advance. :)

Edited by PKG

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Use function "Run" to start "ScrPix.au3". Run returns the PID which can be used to close the process when Stop is being pressed.

BTW: Why do you need to create a screenshot once a second?

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

 

BTW: Why do you need to create a screenshot once a second?

It is just an Experiment. :)

Thanks Water.

Share this post


Link to post
Share on other sites

Or if the script you wish to stop is a compiled one, you can use ProcessClose with it's executable name.


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Share this post


Link to post
Share on other sites

Or use IPC (Inter Process Communication) to send a stop signal to the second script. IPC is easy to implement - one of the best examples uses mailslot (written by trancexx). Search the forum for IPC or Inter process COmmunication and you will get a lot of hits.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Thanks guys. :)

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