【问题标题】:Is the following codes right? Can I make just one with this two codes?下面的代码对吗?我可以用这两个代码只做一个吗?
【发布时间】:2019-07-09 23:57:43
【问题描述】:

我正在尝试编写一个脚本来在晚上 8:00 启动两个应用程序并在早上 8:00 停止它,并在它停止时删除一个目录。 我现在无法测试它,而且我是 AutoIt 的新手,所以如果有人对我的代码有任何建议或知道另一种方法(我尝试使用任务管理器调用批处理文件,但我遇到了一些问题)。我会很高兴有你的帮助! 我的想法是让这个脚本成为一个 exe 并使用任务管理器安排在登录时启动,当晚上 8:00 时它启动两个应用程序(验证 OS Arch)。我需要让这个应用程序在早上 8:00 停止,我正在考虑制作第二个脚本来完成它,但如果可以只用一个脚本制作会更好。

适用于 Windows 机器,x64 或 x86。

FIRST SCRIP: 
#include <Timers.au3>
#NoTrayIcon
#persistent

loop {                           
    If (A_Hour = 20) and (A_Min = 00) { ; is time 8:00pm?
        If @OSArch = "X64" Then 
            RunWait (C:\ProgramData\...\FirstApp.exe)
            Run (C:\ProgramData\...\SecondApp.exe)
        Elseif @OSArch = "X86" Then 
            RunWait (C:\ProgramData\...\FirstApp_x86.exe)
            Run (C:\ProgramData\...\SecondApp_x86.exe)
        EndIf
    }
    sleep, 1000 * 60  ; sleep for 60 seconds so only loop once per minute
}
return

SECOND SCRIPT:
#include <Timers.au3>
#NoTrayIcon
#persistent

loop {
    if (A_Hour = 08) and (A_Min = 00) { ; is time 8:00am?
        Run (@COMSPEC & "taskkill /F /IM FirstApp.exe", @SW_HIDE)
        Run (@COMSPEC & "taskkill /F /IM SecondApp.exe", @SW_HIDE)
        Run (@COMSPEC & "RMDIR C:\ProgramData\MyDir\ /S /Q", @SW_HIDE)
    }
    sleep, 1000 * 60  ; sleep for 60 seconds so only loop once per minute
}
return

【问题讨论】:

  • 我强烈建议使用任务计划程序来启动这些程序。它在 CPU 上将比不断运行程序更可靠、更容易。

标签: autoit


【解决方案1】:

尝试将您的命令放入函数中。

#include <Misc.au3> ; needed for _Singleton

_Singleton(@ScriptName, 0) ; allows one one instance of running script
HotKeySet("{ESC}", _close) ; [optional to exit script]

Do
    Sleep(20)

;waits for 8 AM 
If  @HOUR = "08" And @MIN = "00" And @SEC = "00" Then
    one()
EndIf

;waits for 8 PM 
If  @HOUR = "20" And @MIN = "00" And @SEC = "00"  Then
    two()
EndIf

Until  GUIGetMsg() = -3 ; $GUI_EVENT_CLOSE

Func one()

    ;Add the commmands you want executed at 8AM here

EndFunc   ;==>one

Func two()

    ;Add the commmands you want executed at 8PM here

EndFunc   ;==>two

Func _close()
    Exit
EndFunc

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-23
    • 2011-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多