xkowen 发表于 2008-7-10 18:07:42

执行多条件时单击关闭按钮无效?

小弟初学,请高手帮忙看看这个代码,谢谢了!(附件包含*.ini与*au3文件)
是这样的,我的意图是----运行代码后不做任何动作时就直接执行case GUIGetMsg()=0后的代码!并且是重复执行的!(这步可以实现!)
但是我当单击关闭按钮时就不起作用了!请问怎么解决这个问题啊?谢谢了!

#include <IE.au3>
#include <GUIConstants.au3>
$oi1=IniRead("auto.ini", "auto", "ipaddr", "")
$oi2=IniRead("auto.ini", "auto", "times", "")
$oi3=IniRead("auto.ini", "auto", "sleep", "")
$oi4=IniRead("auto.ini", "auto", "user", "")
$oi5=IniRead("auto.ini", "auto", "password", "")
$oi6=IniRead("auto.ini", "auto", "re", "")
$oi7=IniRead("auto.ini", "auto", "P", "")

#Region ### START Koda GUI section ### Form=f:documents and settingsadministrator桌面form1.kxf
$Form1 = GUICreate("TEST_V0.1", 283, 110, 193, 125)
$Label1 = GUICtrlCreateLabel("欢迎使用自动重启程序", 24, 16, 107, 17)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
$Label2 = GUICtrlCreateLabel("完成了", 64, 48, 88, 17)
$Label4 = GUICtrlCreateLabel("0次!", 152, 48, 120, 17)
$Label5 = GUICtrlCreateLabel("作者:xxx", 216, 88, 57, 17)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
                Case $GUI_EVENT_CLOSE
                          Exit
                case GUIGetMsg()=0
                        If $oi7=1 Then
                    $oi=1
                                While $oi<=$oi2
                                        $oIE1 = _IECreate($oi1,0,0,1,0)
                                        $oForm = _IEFormGetObjByName ($oIE1, "loginForm")
                  $oQuery = _IEFormElementGetObjByName ($oForm, "U2")
                                        _IEFormElementSetValue ($oQuery, $oi4)
                                        $oQuery = _IEFormElementGetObjByName ($oForm, "P2")
                                        _IEFormElementSetValue ($oQuery, $oi5)
                                        Sleep(1000)
                                        _IEFormSubmit ($oForm)
                                   Sleep(1000)
                                   $oIE2 = _IECreate($oi1 & $oi6,0,0,1,0)
                                   _IEQuit($oIE1)
                                   $inputs=_IEGetObjById($oIE2,"idSubmit")
                                   _IEAction($inputs,"click")
                                     Sleep(5000)
                                     _IEQuit($oIE2)               
                                  $oi=$oi+1
                                  If $oi-1=$oi2 Then
                                             GUICtrlSetData( $Label4,$oi-1&"次!")
                                             MsgBox(0,"TEST OVER","执行"&$oi2&"次测试完毕!")
                                             Sleep(2147483647)
                                          ElseIf $oi<=$oi2+1 Then
                                             GUICtrlSetData( $Label4,$oi-1&"次!")
                                  Else
                              MsgBox(0,"!","未知错误!")
                                        EndIf
                      sleep($oi3*1000)
                    WEnd
                        ElseIf $oi7=2 Then
                               $oi=1
                                While $oi<=$oi2
                                       $oie = _IECreate ($oi1,0,0,1,0)
                                       $oform = _IEFormGetObjByName ($oie, "loginForm")
                                       $otext = _IEFormElementGetObjByName ($oform, "P2")
                                        _IEFormElementSetValue ($otext, "admin")
                                       _IEFormSubmit ($oform)
                                       Sleep(1000)
                                    $form2=_IEGetObjByName($oie,"reboot")
                                    _IEAction($form2,"click")
                                    Sleep(2000)
                                    _IEQuit($oie)
                                   $oi=$oi+1
                                  If $oi-1=$oi2 Then
                                       GUICtrlSetData( $Label4,$oi-1&"次!")
                                       MsgBox(0,"TEST OVER","执行"&$oi2&"次测试完毕!")
                                       Sleep(2147483647)
                                  ElseIf $oi<=$oi2+1 Then
                                       GUICtrlSetData( $Label4,$oi-1&"次!")
                                  Else
                                        MsgBox(0,"!","未知错误!")
                                  EndIf
                               sleep($oi3*1000)
                WEnd
            Else
                    MsgBox(0,"提示消息","P值配置错误!请重新检查配置文件!")
                        EndIf
               
        EndSwitch
WEnd
:face (9):

[ 本帖最后由 xkowen 于 2008-7-11 10:37 编辑 ]

顽固不化 发表于 2008-7-10 18:51:09

是呀,用sleep就停止了。

xkowen 发表于 2008-7-10 19:44:11

回复 2# xtybfgu 与 3#顽固不化 的帖子

$oi3是从auto.ini文件中读取出来的,是为了方便用户配置循环的间隔时间!默认是60s(秒)。
sleep($oi3*1000)就等于sleep(60000)
Sleep(2147483647) 是在规定次数执行完成后才会执行的!默认是执行1000次,所以一般不会执行到这行的!
我也觉得sleep用得不好,但又不知道哪个函数可以代替,有没有哪个函数可以带替sleep暂停的?
还有就是先帮我解决无法单击关闭关闭程序的问题吧!谢谢各位哥哥姐姐了!

顽固不化 发表于 2008-7-10 20:40:53

感觉用事件模式比较适合你的意图。

xkowen 发表于 2008-7-10 21:01:27

回复 5# 顽固不化 的帖子

大哥,说说该怎么写吧!

小三 发表于 2008-7-10 21:12:19

#include <IE.au3>
#include <GUIConstants.au3>
$oi1=IniRead("auto.ini", "auto", "ipaddr", "")
$oi2=IniRead("auto.ini", "auto", "times", "")
$oi3=IniRead("auto.ini", "auto", "sleep", "")
$oi4=IniRead("auto.ini", "auto", "user", "")
$oi5=IniRead("auto.ini", "auto", "password", "")
$oi6=IniRead("auto.ini", "auto", "re", "")
$oi7=IniRead("auto.ini", "auto", "P", "")

#Region ### START Koda GUI section ### Form=f:documents and settingsadministrator桌面form1.kxf
$Form1 = GUICreate("TEST_V0.1", 283, 110, 193, 125)
$Label1 = GUICtrlCreateLabel("欢迎使用自动重启程序", 24, 16, 107, 17)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
$Label2 = GUICtrlCreateLabel("完成了", 64, 48, 88, 17)
$Label4 = GUICtrlCreateLabel("0次!", 152, 48, 120, 17)
$Label5 = GUICtrlCreateLabel("作者:xxx", 216, 88, 57, 17)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
While 1
      $nMsg = GUIGetMsg()
      Switch $nMsg
                case GUIGetMsg()=0
if $nMsg = $GUI_EVENT_CLOSE Then
Exit
Else
_test()
endif
      EndSwitch
WEnd

func _test()
                        If $oi7=1 Then
                  $oi=1
                              While $oi<=$oi2
                                        $oIE1 = _IECreate($oi1,0,0,1,0)
                                        $oForm = _IEFormGetObjByName ($oIE1, "loginForm")
                  $oQuery = _IEFormElementGetObjByName ($oForm, "U2")
                                        _IEFormElementSetValue ($oQuery, $oi4)
                                        $oQuery = _IEFormElementGetObjByName ($oForm, "P2")
                                        _IEFormElementSetValue ($oQuery, $oi5)
                                        Sleep(1000)
                                        _IEFormSubmit ($oForm)
                                       Sleep(1000)
                                       $oIE2 = _IECreate($oi1 & $oi6,0,0,1,0)
                                       _IEQuit($oIE1)
                                       $inputs=_IEGetObjById($oIE2,"idSubmit")
                                       _IEAction($inputs,"click")
                                       Sleep(5000)
                                       _IEQuit($oIE2)               
                                    $oi=$oi+1
                                    If $oi-1=$oi2 Then
                                             GUICtrlSetData( $Label4,$oi-1&"次!")
                                             MsgBox(0,"TEST OVER","执行"&$oi2&"次测试完毕!")
                                             Sleep(2147483647)
                                          ElseIf $oi<=$oi2+1 Then
                                             GUICtrlSetData( $Label4,$oi-1&"次!")
                                    Else
                              MsgBox(0,"!","未知错误!")
                                        EndIf
                        sleep($oi3*1000)
                      WEnd
                        ElseIf $oi7=2 Then
                                 $oi=1
                              While $oi<=$oi2
                                       $oie = _IECreate ($oi1,0,0,1,0)
                                       $oform = _IEFormGetObjByName ($oie, "loginForm")
                                       $otext = _IEFormElementGetObjByName ($oform, "P2")
                                        _IEFormElementSetValue ($otext, "admin")
                                       _IEFormSubmit ($oform)
                                       Sleep(1000)
                                    $form2=_IEGetObjByName($oie,"reboot")
                                    _IEAction($form2,"click")
                                    Sleep(2000)
                                    _IEQuit($oie)
                                 $oi=$oi+1
                                  If $oi-1=$oi2 Then
                                       GUICtrlSetData( $Label4,$oi-1&"次!")
                                       MsgBox(0,"TEST OVER","执行"&$oi2&"次测试完毕!")
                                       Sleep(2147483647)
                                  ElseIf $oi<=$oi2+1 Then
                                       GUICtrlSetData( $Label4,$oi-1&"次!")
                                  Else
                                        MsgBox(0,"!","未知错误!")
                                  EndIf
                                 sleep($oi3*1000)
                WEnd
            Else
;~                     MsgBox(0,"提示消息","P值配置错误!请重新检查配置文件!")
                        EndIf
               
EndFunc

xkowen 发表于 2008-7-10 23:06:59

回复 7# 小三 的帖子

大哥,你修改的这个代码貌似运行起来后还是不行啊!关闭按钮仍然无效哇...
谢谢赐教!学到了另一种方法!
还有哪位高手知道该怎么写啊?

顽固不化 发表于 2008-7-10 23:52:24

关键是你处理的那部分耗用太多时间,没有时间来处理主循环的关闭事件。

给你举个例子,尽管循环在不停执行那个60秒的自定义函数,但只要有关闭事件,程序立即退出。

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
Opt("GUIOnEventMode", 1) ;========>注意这句

$form = GUICreate("test")
GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked") ;========>注意这句
GUISetState(@SW_SHOW)

While 1
        myfunc()
WEnd


Func myfunc()
        sleep(60000)
EndFunc   ;==>myfunc

Func CLOSEClicked()
   Exit
EndFunc

[ 本帖最后由 顽固不化 于 2008-7-11 00:05 编辑 ]

cxlater 发表于 2008-7-11 01:49:48

你的问题以前我也遇到过    当使用sleep的时候 主循环已经停止gui已经获取不到控件的事件信息
考虑一下使用多线程可能能解决问题

小三 发表于 2008-7-11 02:23:15

回复 9# 顽固不化 的帖子

帮他改一下

#include <IE.au3>
#include <GUIConstants.au3>

Opt("GUIOnEventMode", 1) ;========>注意这句
$oi1=IniRead("auto.ini", "auto", "ipaddr", "")
$oi2=IniRead("auto.ini", "auto", "times", "")
$oi3=IniRead("auto.ini", "auto", "sleep", "")
$oi4=IniRead("auto.ini", "auto", "user", "")
$oi5=IniRead("auto.ini", "auto", "password", "")
$oi6=IniRead("auto.ini", "auto", "re", "")
$oi7=IniRead("auto.ini", "auto", "P", "")

#Region ### START Koda GUI section ### Form=f:documents and settingsadministrator桌面form1.kxf
$Form1 = GUICreate("TEST_V0.1", 283, 110, 193, 125)
GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked") ;========>注意这句
$Label1 = GUICtrlCreateLabel("欢迎使用自动重启程序", 24, 16, 107, 17)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
$Label2 = GUICtrlCreateLabel("完成了", 64, 48, 88, 17)
$Label4 = GUICtrlCreateLabel("0次!", 152, 48, 120, 17)
$Label5 = GUICtrlCreateLabel("作者:xxx", 216, 88, 57, 17)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
                Case $GUI_EVENT_CLOSE
                          Exit
                case GUIGetMsg()=0
                        If $oi7=1 Then
                    $oi=1
                                While $oi<=$oi2
                                        $oIE1 = _IECreate($oi1,0,0,1,0)
                                        $oForm = _IEFormGetObjByName ($oIE1, "loginForm")
                  $oQuery = _IEFormElementGetObjByName ($oForm, "U2")
                                        _IEFormElementSetValue ($oQuery, $oi4)
                                        $oQuery = _IEFormElementGetObjByName ($oForm, "P2")
                                        _IEFormElementSetValue ($oQuery, $oi5)
                                        Sleep(1000)
                                        _IEFormSubmit ($oForm)
                                        Sleep(1000)
                                        $oIE2 = _IECreate($oi1 & $oi6,0,0,1,0)
                                        _IEQuit($oIE1)
                                        $inputs=_IEGetObjById($oIE2,"idSubmit")
                                        _IEAction($inputs,"click")
                                        Sleep(5000)
                                        _IEQuit($oIE2)               
                                  $oi=$oi+1
                                  If $oi-1=$oi2 Then
                                             GUICtrlSetData( $Label4,$oi-1&"次!")
                                             MsgBox(0,"TEST OVER","执行"&$oi2&"次测试完毕!")
                                             Sleep(2147483647)
                                          ElseIf $oi<=$oi2+1 Then
                                             GUICtrlSetData( $Label4,$oi-1&"次!")
                                  Else
                              MsgBox(0,"!","未知错误!")
                                        EndIf
                      sleep($oi3*1000)
                    WEnd
                        ElseIf $oi7=2 Then
                                        $oi=1
                                While $oi<=$oi2
                  $oie = _IECreate ($oi1,0,0,1,0)
                                        $oform = _IEFormGetObjByName ($oie, "loginForm")
                  $otext = _IEFormElementGetObjByName ($oform, "P2")
                  _IEFormElementSetValue ($otext, "admin")
                  _IEFormSubmit ($oform)
                  Sleep(1000)
                                        $form2=_IEGetObjByName($oie,"reboot")
                                        _IEAction($form2,"click")
                            Sleep(2000)
                                        _IEQuit($oie)
                      $oi=$oi+1
                                        If $oi-1=$oi2 Then
                                                GUICtrlSetData( $Label4,$oi-1&"次!")
                                                MsgBox(0,"TEST OVER","执行"&$oi2&"次测试完毕!")
                                                Sleep(2147483647)
                                        ElseIf $oi<=$oi2+1 Then
                                                GUICtrlSetData( $Label4,$oi-1&"次!")
                                        Else
                                                MsgBox(0,"!","未知错误!")
                                        EndIf
                                        sleep($oi3*1000)
                WEnd
            Else
                    MsgBox(0,"提示消息","P值配置错误!请重新检查配置文件!")
                        EndIf
               
        EndSwitch
WEnd

Func CLOSEClicked()
   Exit
EndFunc

xkowen 发表于 2008-7-11 09:54:32

回复 10# cxlater 的帖子

那你是怎么解决这种问题的呢?给大家分享一下!

xkowen 发表于 2008-7-11 10:04:59

回复 11# 小三 的帖子

辛苦你了,这么晚还回帖!谢谢你!
我刚刚查阅了资料,OnEvent 函数仅当选项 GUIOnEventMode 的值为1时才能被调用,而在此模式下 GUIGetMsg 将完全无效!所以你给的代码中要去掉$nMsg = GUIGetMsg()相关函数才正确!

正确的应该是这样的!大家可以看看,希望对象我这种初学者有所帮助!
#include <IE.au3>
#include <GUIConstants.au3>
$oi1=IniRead("auto.ini", "auto", "ipaddr", "")
$oi2=IniRead("auto.ini", "auto", "times", "")
$oi3=IniRead("auto.ini", "auto", "sleep", "")
$oi4=IniRead("auto.ini", "auto", "user", "")
$oi5=IniRead("auto.ini", "auto", "password", "")
$oi6=IniRead("auto.ini", "auto", "re", "")
$oi7=IniRead("auto.ini", "auto", "P", "")
Opt("GUIOnEventMode", 1)
#Region ### START Koda GUI section ### Form=f:documents and settingsadministrator桌面form1.kxf
$Form1 = GUICreate("GXE_TEST_V0.1", 283, 110, 193, 125)
$Label1 = GUICtrlCreateLabel("欢迎使用自动重启程序", 24, 16, 150, 17)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
$Label2 = GUICtrlCreateLabel("已经完成了", 64, 48, 88, 17)
$Label4 = GUICtrlCreateLabel("0次!", 152, 48, 120, 17)
$Label5 = GUICtrlCreateLabel("作者:xxx", 216, 88, 57, 17)
GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
While 1
        test()
WEnd
Func test()
If $oi7=1 Then
        $oi=1
    While $oi<=$oi2
           $oIE1 = _IECreate($oi1,0,0,1,0)
         $oForm = _IEFormGetObjByName ($oIE1, "loginForm")
         $oQuery = _IEFormElementGetObjByName ($oForm, "U2")
         _IEFormElementSetValue ($oQuery, $oi4)
          $oQuery = _IEFormElementGetObjByName ($oForm, "P2")
          _IEFormElementSetValue ($oQuery, $oi5)
          Sleep(1000)
          _IEFormSubmit ($oForm)
      Sleep(1000)
      $oIE2 = _IECreate($oi1 & $oi6,0,0,1,0)
      _IEQuit($oIE1)
      $inputs=_IEGetObjById($oIE2,"idSubmit")
      _IEAction($inputs,"click")
                Sleep(5000)
                _IEQuit($oIE2)               
          $oi=$oi+1
          If $oi-1=$oi2 Then
                   GUICtrlSetData( $Label4,$oi-1&"次!")
                   MsgBox(0,"TEST OVER","执行"&$oi2&"次测试完毕!")
                   Sleep(2147483647)
           ElseIf $oi<=$oi2+1 Then
                   GUICtrlSetData( $Label4,$oi-1&"次!")
          Else
                   MsgBox(0,"!","未知错误!")
          EndIf
          sleep($oi3*1000)
        WEnd
;#################################################################       
ElseIf $oi7=2 Then
        $oi=1
    While $oi<=$oi2
      $oie = _IECreate ($oi1,0,0,1,0)
      $oform = _IEFormGetObjByName ($oie, "loginForm")
      $otext = _IEFormElementGetObjByName ($oform, "P2")
      _IEFormElementSetValue ($otext, "admin")
      _IEFormSubmit ($oform)
      Sleep(1000)
      $form2=_IEGetObjByName($oie,"reboot")
      _IEAction($form2,"click")
                Sleep(2000)
                _IEQuit($oie)
          $oi=$oi+1
          If $oi-1=$oi2 Then
                   GUICtrlSetData( $Label4,$oi-1&"次!")
                   MsgBox(0,"TEST OVER","执行"&$oi2&"次测试完毕!")
                   Sleep(2147483647)
                ElseIf $oi<=$oi2+1 Then
                   GUICtrlSetData( $Label4,$oi-1&"次!")
          Else
                   MsgBox(0,"!","未知错误!")
          EndIf
          sleep($oi3*1000)
    WEnd
Else
        MsgBox(0,"提示消息","P值配置错误!请重新检查配置文件!")
EndIf
EndFunc
Func CLOSEClicked()
        Exit
EndFunc

xkowen 发表于 2008-7-11 10:07:59

回复 9# 顽固不化 的帖子

谢谢!你给的这种方法是正确的...
偶刚开始学,所以没有这样用过,看来以后还要努力学习啊!
:face (29):

waynelue 发表于 2008-10-31 14:38:52

555555555,终于给我找到了,原来真的是SLEEP与主循环的问题,难怪我昨晚搞了一整晚都没搞好。。。太气人了。。。哈哈
页: [1]
查看完整版本: 执行多条件时单击关闭按钮无效?