Jump to content

Need help on using GUIonEventMode <solved>


lhw
 Share

Recommended Posts

hello guys, i'm trying to add buttons on my program so that add features can control the process,it seem GuiOnEventMode is the best choice for my program,the follows are main parts of my program included four functions work for me very well if not put them together,can someone point out where i am wrong or there is better way to handle this,thanks in advance.

#include <array.au3>
#include <GUIConstantsEx.au3>
#include <ProgressConstants.au3>

$MainGUI = GUICreate("Test", 220, 115)
$progressbar = GUICtrlCreateProgress(10, 10, 200, 20)
$btn1 = GUICtrlCreateButton("Run", 160, 91, 50, 20)
$btn2 = GUICtrlCreateButton("Suspend", 10, 35, 50, 20)
$btn3 = GUICtrlCreateButton("Resume", 10, 63, 50, 20)
$btn4 = GUICtrlCreateButton("Terminate", 10, 91, 50, 20)
Opt("GUIOnEventMode", 1)
GUISetState()
    GUISetOnEvent($GUI_EVENT_CLOSE, "_Main")
    GUICtrlSetOnEvent($btn1, "_Main")
    GUICtrlSetOnEvent($btn2, "_Main")
    GUICtrlSetOnEvent($btn3, "_Main")
    GUICtrlSetOnEvent($btn4, "_Main")

While 1
    Sleep(20)
WEnd

Func _Main()
    Switch @GUI_CtrlId
        Case $GUI_EVENT_CLOSE
            Exit
        Case $BTN1
            $source = 'C:\data'
            $Target = 'C:\test.7z'
            $cline = ' a -t7z -mx=9 ' & '"'& $target & '"' & ' ' & '"'& $source  & '"' & '\*'
            $Total  = DirGetSize('C:\data')
            _GetPIOData('7z.exe', $cline, $Total, $progressbar)
        Case $btn2
            $s = _ProcessSuspend('7z.exe')
            ConsoleWrite('--S->--'& $s & @LF)
        Case $btn3
            $r = _ProcessResume('7z.exe')
            ConsoleWrite('--R->--'& $r & @LF)
        Case $btn4
            $t = _ProcessTerminate('7z.exe')
            ConsoleWrite('--T->--'& $t & @LF)
    EndSwitch
EndFunc

Func _GetPIOData($Execute, $Commandline, $Param, $Ctrl)
    Local $Pid, $PIOData, $iPercentage, $iPercentageBefore
    $Pid = Run ($Execute & $Commandline, '', @SW_HIDE)
    While ProcessExists($Pid)
        $PIOData = ProcessGetStats($Execute, 1)
        If @error Then
           MsgBox(0, '', 'Cant get IO info from current process')
        Else
            $iPercentage = Round($PIOData[3]/$Param*100)
            If $iPercentage <> $iPercentageBefore And $iPercentage > 0 And $iPercentage <= 100 Then
               ConsoleWrite('>>>>' & $iPercentage & @CRLF)
               GUICtrlSetData($Ctrl , $iPercentage)
            EndIf
        EndIf
    WEnd
    Return $iPercentage
EndFunc

Func _ProcessSuspend($process)
$processid = ProcessExists($process)
If $processid Then
    $ai_Handle = DllCall("kernel32.dll", 'int', 'OpenProcess', 'int', 0x1f0fff, 'int', False, 'int', $processid)
    $i_sucess = DllCall("ntdll.dll","int","NtSuspendProcess","int",$ai_Handle[0])
    DllCall('kernel32.dll', 'ptr', 'CloseHandle', 'ptr', $ai_Handle)
    If IsArray($i_sucess) Then 
        Return 1
    Else
        SetError(1)
        Return 0
    Endif
Else
    SetError(2)
    Return 0
Endif
EndFunc

Func _ProcessResume($process)
$processid = ProcessExists($process)
If $processid Then
    $ai_Handle = DllCall("kernel32.dll", 'int', 'OpenProcess', 'int', 0x1f0fff, 'int', False, 'int', $processid)
    $i_sucess = DllCall("ntdll.dll","int","NtResumeProcess","int",$ai_Handle[0])
    DllCall('kernel32.dll', 'ptr', 'CloseHandle', 'ptr', $ai_Handle)
    If IsArray($i_sucess) Then 
        Return 1
    Else
        SetError(1)
        Return 0
    Endif
Else
    SetError(2)
    Return 0
Endif
EndFunc

Func _ProcessTerminate($process)
$processid = ProcessExists($process)
If $processid Then
    $ai_Handle = DllCall("kernel32.dll", 'int', 'OpenProcess', 'int', 0x1f0fff, 'int', False, 'int', $processid)
    $i_sucess = DllCall("ntdll.dll","int","NtTerminateProcess","int",$ai_Handle[0])
    DllCall('kernel32.dll', 'ptr', 'CloseHandle', 'ptr', $ai_Handle)
    If IsArray($i_sucess) Then 
        Return 1
    Else
        SetError(1)
        Return 0
    Endif
Else
    SetError(2)
    Return 0
Endif
EndFunc
Edited by lhw
Link to comment
Share on other sites

  • Moderators

lhw,

Are you asking why the "Suspend", "Resume" and "Terminate" buttons do not work while the process is running? :unsure:

If so, this is becasue you remain in the _GetPIOData function until the 7z process ends and other functions will not run until you return to the idle loop. :>

Look at the Interrupting a running function tutorial in the Wiki to learn a number of ways to get around this. ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

lhw,

Are you asking why the "Suspend", "Resume" and "Terminate" buttons do not work while the process is running? :unsure:

If so, this is becasue you remain in the _GetPIOData function until the 7z process ends and other functions will not run until you return to the idle loop. :>

Look at the Interrupting a running function tutorial in the Wiki to learn a number of ways to get around this. ;)

M23

yes, my problem just like that, thank you for the WIKI link , already got some hope from there:party:, need some time to understand them

Link to comment
Share on other sites

  • Moderators

lhw,

Come back and ask again if you run into difficulties. :unsure:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

lhw,

Come back and ask again if you run into difficulties. :>

M23

thanks for your patience

my script comes to works in MessageLoop mode,will try Guioneventmode later,thanks again :unsure:

#include <array.au3>
#include <GUIConstantsEx.au3>
#include <ProgressConstants.au3>
#include <WindowsConstants.au3>

$MainGUI = GUICreate("Test", 220, 115)
$progressbar = GUICtrlCreateProgress(10, 10, 200, 20)
$btn1 = GUICtrlCreateButton("Run", 160, 90, 50, 20)
$btn2 = GUICtrlCreateButton("Abort", 90, 90, 50, 20)
GUICtrlSetState($btn2, $GUI_DISABLE)
GUISetState()
GUIRegisterMsg($WM_COMMAND, "_WM_COMMAND")

            $source = 'C:\data'
            $Target = 'C:\test.7z'
            $cline = ' a -t7z -mx=9 ' & '"'& $target & '"' & ' ' & '"'& $source  & '"' & '\*'
            $Total  = DirGetSize('C:\data')
            
$fInterrupt = 0

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $btn1
            GUICtrlSetState($btn1, $GUI_DISABLE)
            GUICtrlSetState($btn2, $GUI_ENABLE)
            Run ('7za.exe ' & $cline, '', @SW_HIDE)
            _GETPIOData()
        Case $btn2
            _ProcessPauseSwitch('7za.exe', true)
            $iMsgBoxAnswer = MsgBox(68, "Prompt", "Do you want abort current task?")
                Select
                    Case $iMsgBoxAnswer = 6 ;Yes
                        ProcessClose('7za.exe')
                        GUICtrlSetData($progressbar, 0)
                        GUICtrlSetState($btn2, $GUI_DISABLE)
                        GUICtrlSetState($btn1, $GUI_ENABLE)
                        MsgBox (64, 'info', 'task aborted', 2)
                    Case $iMsgBoxAnswer = 7 ;No
                        _ProcessPauseSwitch('7za.exe', False)
                        _GETPIOData()
                EndSelect

    EndSwitch
WEnd

Func _GETPIOData()
    Dim $PIOData, $iPercentage, $iPercentageBefore
    $fInterrupt = 0 
        While ProcessExists('7za.exe')
            $PIOData= ProcessGetStats('7za.exe', 1)
            $iPercentage = Round ($PIOData[3]/$Total*100)
            If $iPercentage > $iPercentageBefore And $iPercentage > 0 And $iPercentage <= 100 Then 
            GUICtrlSetData($progressbar, $iPercentage)
            EndIf
            If $fInterrupt <> 0 Then    
                Switch $fInterrupt  
                    Case 1
                    Case 2
                EndSwitch
                Return $fInterrupt
            EndIf
        WEnd
        GUICtrlSetData($progressbar, 100)
        MsgBox(0, '', 'Successfully')
        GUICtrlSetData($progressbar, 0)
        GUICtrlSetState($btn2, $GUI_DISABLE)
        GUICtrlSetState($btn1, $GUI_ENABLE)
EndFunc


Func _WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
If BitAND($wParam, 0x0000FFFF) = $btn1 Then $fInterrupt = 1
If BitAND($wParam, 0x0000FFFF) = $btn2 Then $fInterrupt = 2
Return $GUI_RUNDEFMSG
EndFunc;==>_WM_COMMAND

Func _ProcessPauseSwitch($iPIDOrName, $iSuspend = True);http://www.autoitscript.com/forum/topic/50708-stop-process/page__view__findpost__p__383631
    If IsString($iPIDOrName) Then $iPIDOrName = ProcessExists($iPIDOrName)
    If Not $iPIDOrName Then Return SetError(1, 0, 0)
    Local $ai_Handle = DllCall("kernel32.dll", 'int', 'OpenProcess', 'int', 0x1f0fff, 'int', False, 'int', $iPIDOrName)
    If $iSuspend Then
        Local $i_sucess = DllCall("ntdll.dll", "int", "NtSuspendProcess", "int", $ai_Handle[0])
    Else
        Local $i_sucess = DllCall("ntdll.dll", "int", "NtResumeProcess", "int", $ai_Handle[0])
    EndIf
    DllCall('kernel32.dll', 'ptr', 'CloseHandle', 'ptr', $ai_Handle)
    If IsArray($i_sucess) Then Return 1
    Return SetError(2, 0, 0)
EndFunc
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...