MrCreatoR Posted April 23, 2020 Posted April 23, 2020 (edited) Allows to execute function (UDF or built-in) without pausing the main script, using callback handler function to receive the return. Notes: Quote * This UDF registers $WM_COPYDATA, if you or other UDF uses this message, __FE_WM_COPYDATA($hWnd, $iMsg, $wParam, $lParam) should be called from that other message function. * This UDF uses #NoTrayIcon, to show the tray icon in your script use Opt('TrayIconHide', 0). Example (Simple): #include 'FuncExecute.au3' _FuncExecute_Start('_MyCalcFunc', '3', '_Callback', 1) _FuncExecute_Start('InputBox', 'Title|Prompt:|Text', '_Callback', 2) _FuncExecute_Start('MsgBox', '52|FuncExecute Example #1|Simple message box, OK?', '_Callback', 3) While _FuncExecute_Count() > 0 Sleep(100) WEnd ;This function get triggered only after the called function has finished Func _Callback($sRet, $iErr, $iPID, $sFunc, $sParams, $vParam) Local $sMsg = StringFormat('External call...\n\tPID = %i\n\tCallback Param = %s\n\tFunc = %s\n\tParams: %s\nReturn:\n\t%s%s\nError:\n\t%i', _ $iPID, $vParam, $sFunc, $sParams, $sRet, ($sRet = 6 ? ' (OK)' : ''), $iErr) ConsoleWrite($sMsg & @CRLF & '================================' & @CRLF & @CRLF) EndFunc Func _MyCalcFunc($iNumber) Return $iNumber & ' Is Even = ' & (Mod($iNumber, 2) = 0) EndFunc Example (Advanced): expandcollapse popup#include <GUIConstantsEx.au3> #include <ListViewConstants.au3> #include <WinAPIShPath.au3> #include 'FuncExecute.au3' Global $aDirs[][3] = [[_WinAPI_PathCanonicalize(@AutoItExe & '\..')], [@ProgramFilesDir], [@WindowsDir], [@TempDir], [@ScriptDir]] $hGUI = GUICreate('FuncExecute Example #2', 500, 400) $iStart_Bttn = GUICtrlCreateButton('Get Size', 20, 20, 70, 20) $iStop_Bttn = GUICtrlCreateButton('Stop', 100, 20, 70, 20) GUICtrlSetState($iStop_Bttn, $GUI_DISABLE) $iLV = GUICtrlCreateListView('Path|Size', 20, 50, 460, 310) For $i = 0 To UBound($aDirs) - 1 $aDirs[$i][1] = GUICtrlCreateListViewItem($aDirs[$i][0] & '|N/A', $iLV) Next GUICtrlSendMsg($iLV, $LVM_SETCOLUMNWIDTH, 0, 350) GUICtrlSendMsg($iLV, $LVM_SETCOLUMNWIDTH, 1, 100) $iStatus_Lbl = GUICtrlCreateLabel('Press "Get Size" Button', 20, 370, 460, 20) GUICtrlSetColor($iStatus_Lbl, 0xFF0000) GUISetState(@SW_SHOW, $hGUI) While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE For $i = 0 To UBound($aDirs) - 1 _FuncExecute_Stop($aDirs[$i][2]) Next Exit Case $iStart_Bttn GUICtrlSetState($iStop_Bttn, $GUI_ENABLE) GUICtrlSetState($iStart_Bttn, $GUI_DISABLE) GUICtrlSetData($iStatus_Lbl, 'Please wait, collecting data...') For $i = 0 To UBound($aDirs) - 1 GUICtrlSetData($aDirs[$i][1], $aDirs[$i][0] & '|Wait...') $aDirs[$i][2] = _FuncExecute_Start('DirGetSize', $aDirs[$i][0], '_DirSetSize_Callback', $aDirs[$i][1]) Next Case $iStop_Bttn If MsgBox(52, 'Attention', 'Are you sure, Stop collecting data?', 0, $hGUI) <> 6 Then ContinueLoop EndIf For $i = 0 To UBound($aDirs) - 1 If _FuncExecute_Stop($aDirs[$i][2]) Then GUICtrlSetData($aDirs[$i][1], $aDirs[$i][0] & '|N/A') EndIf Next GUICtrlSetData($iStatus_Lbl, 'Press "Get Size" Button') GUICtrlSetState($iStop_Bttn, $GUI_DISABLE) GUICtrlSetState($iStart_Bttn, $GUI_ENABLE) EndSwitch WEnd Func _DirSetSize_Callback($iSize, $iErr, $iPID, $sFunc, $sParams, $iLVItemID) GUICtrlSetData($iLVItemID, $sParams & '|' & _ByteSuffix($iSize)) If _FuncExecute_Count() <= 1 Then GUICtrlSetData($iStatus_Lbl, 'Done!') GUICtrlSetState($iStop_Bttn, $GUI_DISABLE) GUICtrlSetState($iStart_Bttn, $GUI_ENABLE) EndIf EndFunc Func _ByteSuffix($iBytes) Local $iIndex = 0, $aArray = ['bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'] While $iBytes > 1023 $iIndex += 1 $iBytes /= 1024 WEnd Return Round($iBytes, 2) & ' ' & $aArray[$iIndex] EndFunc History version: Quote v0.3 * Now by default the tray icon is hidden, to show it use Opt('TrayIconHide', 0). $FE_SHOWTRAYICON removed. * Examples changed. * Better interaction handling. Now Structures used (instead of StdOut) to pass the return value from called function. * Added _FuncExecute_Count function to get the total count of the started functions. * Added _FuncExecute_GetInfo function to get the info about the started function(s). * Added support to accept 1D array from called function. v0.2 * UDF renamed to FuncExecute. * Examples changed. * Better receiver handling (WM_COPYDATA usage). * $sParams parameter in _FuncExecute_Start renamed to $sFuncParams. + Added optional parameter $vCallbackParam to _FuncExecute_Start function. v0.1 * First public version. FuncExecute_v0.3.zip Edited April 23, 2020 by MrCreatoR Spoiler Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1 AutoIt Russian Community My Work... Spoiler Projects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize ProgramUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF Examples: ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating ) * === My topics === * ================================================== ================================================== AutoIt is simple, subtle, elegant. © AutoIt Team
ripdad Posted April 23, 2020 Posted April 23, 2020 MrCreatoR, Thanks for this UDF -- I will put it to good use! "The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now