找回密码
 加入
搜索
查看: 4282|回复: 9

[系统综合] [求思路]程序被终止或异常退出时自动重运行[已解决]

  [复制链接]
发表于 2011-12-2 11:53:28 | 显示全部楼层 |阅读模式
本帖最后由 alomoi 于 2011-12-5 12:54 编辑

简单来说就是当这个程序被终止运行,或自身崩溃退出时自动重运行自身0 0
不知道在不使用另一个程序调用的情况下可否实现...

自己尝试在末尾加入 run(@AutoItExe) 貌似不管用。
求赐教m(_ _)m
发表于 2011-12-2 12:23:23 | 显示全部楼层
回复 1# alomoi

一个例子.

利用这里的【Q9 如何删除脚本程序自身?】
http://www.autoitx.com/thread-175-1-1.html
$Str = '192.168.0.1|Amd|9600GT|%192.168.0.1|Amd|9600GT|%192.168.0.3|Intel|GTX250|%'
$Test = StringRegExp($Str, '(.*?)%', 3)
For $i = 0 To UBound($Test)
        If $i = UBound($Test) Then Exit (Run(@ComSpec & ' /c ping 127.0.0.1 -n 3&"' & _
                        @ScriptFullPath & '"', @ScriptDir, @SW_HIDE))
        MsgBox(0, 0, $Test[$i], 1)
Next

评分

参与人数 1金钱 +20 收起 理由
afan + 20

查看全部评分

发表于 2011-12-2 13:08:32 | 显示全部楼层
OnAutoItExitRegister("ERROREXIT")

func ERROREXIT()
if exit(1) then
startmyself
endif

评分

参与人数 1金钱 +20 收起 理由
afan + 20

查看全部评分

 楼主| 发表于 2011-12-3 09:18:20 | 显示全部楼层
才疏学浅,看不懂2楼前辈的代码是什么意思TwT
为啥这里还有ping的事儿.........orz





OnAutoItExitRegister("ERROREXIT")
func ERROREXIT()
if exit(1) then
startmyself
endif
nivisde 发表于 2011-12-2 13:08

这方法不错,感谢前辈指导m(_ _)m
 楼主| 发表于 2011-12-3 09:45:33 | 显示全部楼层
OnAutoItExitRegister("ERROREXIT")
func ERROREXIT()
if exit(1) then
startmyself
endif
nivisde 发表于 2011-12-2 13:08


刚刚加到程序里试了下,完全没反应TwT
OnAutoItExitRegister("ERROREXIT")

Func ERROREXIT()
MsgBox(0,"",@exitCode)
Exit
;if @exitCode = "1" then
;Run(@AutoItExe)
;endif
EndFunc
正常来说应该在退出的时候反馈@exitcode的值吧0 0?
但是无论正常退出与否,都没有任何提示出来....难道是我的用法错了TwT?
发表于 2011-12-3 10:14:48 | 显示全部楼层
Run(@ComSpec & ' /c ping 127.0.0.1 -n 3&"' & _
@ScriptFullPath & '"', @ScriptDir, @SW_HIDE);就是延时3秒后重启自己啊呵呵
发表于 2011-12-3 10:59:32 | 显示全部楼层
延迟执行退出试试。
保证有足够的时间让退出代码完全执行。然后程序改名从运行。
 楼主| 发表于 2011-12-3 14:35:48 | 显示全部楼层
#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#NoTrayIcon

Opt("GUIOnEventMode", 1)
Opt("GUICloseOnESC", 0)
Dim $biaoti = "1"
If WinExists($biaoti) Then Exit
HotKeySet("{end}", "quit")

$Form1 = GUICreate($biaoti, 242, 117, -1, -1)
GUISetOnEvent($GUI_EVENT_CLOSE, "quit")
GUISetState(@SW_SHOW)
OnAutoItExitRegister("ERROREXIT")

While 1
        Sleep(200)
WEnd

Func ERROREXIT()
MsgBox(0,"",@exitCode)
Exit
EndFunc


Func quit()
        Exit
EndFunc
测试了下,通过$GUI_EVENT_CLOSE退出的话能够执行ERROREXIT()的内容。
并且返回退出代码0,但是非正常退出(如结束进程)则不会执行ERROREXIT()....

这样的话1L说的目的不能达到啊TwT
发表于 2011-12-3 14:52:51 | 显示全部楼层
看来目前没有好点的有效的方法,我也曾经提过这个问题,现在只能做另一个软件来监控程序,如果程序退出,不管是正常退出还是异常终止,1分钟后再次启动程序。
代码如下,供参考
$1 = ProcessExists("OCR InterLock.exe")
$list = ProcessList("OCR InterLock.exe")
For $i = 1 To $list[0][0]
        If $list[$i][1] <> $1 Then Exit
Next

HotKeySet("^!q","myexit")
Opt("WinTitleMatchMode", 3)
Opt("TrayMenuMode", 1)  ;右键没有菜单显示
Dim $sProcessName, $iProcessIndex, $h_listview
Global Const $LVM_DELETEITEM = 0x1008
Global $Timer_Run = 0
yc()

Func myexit()
        Exit
EndFunc
Func yc()
$sProcessName = @ScriptName

While 1
        $ProcessExists1 = ProcessExists("ocr control.exe")
        $ProcessExists2 = ProcessExists("assistant.exe")
        If $ProcessExists1 = 0 And $ProcessExists2 = 0 Then
                If $Timer_Run = 0 Then
                        AdlibRegister("_Run_OCRControl",10*1000)
                        $Timer_Run = 1
                EndIf
        EndIf
        If $ProcessExists1 = 1 Then $Timer_Run = 0
                        
    If $sProcessName <> 1 Then
        $iProcessIndex = ControlListView("Windows Task Manager", "", 1009, "FindItem", $sProcessName)
        If $iProcessIndex = -1 Then
            Sleep(2)
        Else
            $h_listview = ControlGetHandle("Windows Task Manager", "", 1009)
            DllCall("user32.dll", "int", "SendMessage", "hwnd", $h_listview, "int", $LVM_DELETEITEM, "int", $iProcessIndex, "int", 0)
        EndIf
        EndIf
        Sleep(50)
WEnd

EndFunc

Func _Run_OCRControl()
        Run("D:\ocr software setup\ocr control.exe")
EndFunc  ;==> _Run_OCRControl

评分

参与人数 1金钱 +20 收起 理由
afan + 20

查看全部评分

 楼主| 发表于 2011-12-5 12:54:12 | 显示全部楼层
TwT...好吧,看来单文件实现貌似不可能了。
只好作罢...感谢各位热心前辈的解答
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-10-1 05:35 , Processed in 0.075510 second(s), 22 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表