Jump to content

Search the Community

Showing results for tags 'fork'.

  • Search By Tags

    Type tags separated by commas.
  • Search By Author

Content Type


Forums

  • General
    • Announcements and Site News
    • Administration
  • AutoIt v3
    • AutoIt Help and Support
    • AutoIt Technical Discussion
    • AutoIt Example Scripts
  • Scripting and Development
    • Developer General Discussion
    • Language Specific Discussion
  • IT Administration
    • Operating System Deployment
    • Windows Client
    • Windows Server
    • Office

Categories

  • AutoIt Team
    • Beta
    • MVP
  • AutoIt
    • Automation
    • Databases and web connections
    • Data compression
    • Encryption and hash
    • Games
    • GUI Additions
    • Hardware
    • Information gathering
    • Internet protocol suite
    • Maths
    • Media
    • PDF
    • Security
    • Social Media and other Website API
    • Windows
  • Scripting and Development
  • IT Administration
    • Operating System Deployment
    • Windows Client
    • Windows Server
    • Office

Find results in...

Find results that contain...


Date Created

  • Start

    End


Last Updated

  • Start

    End


Filter by number of...

Joined

  • Start

    End


Group


Member Title


Location


WWW


Interests

Found 3 results

  1. #include <WinAPISysWin.au3> #include <APISysConstants.au3> #include <WinAPISys.au3> #include <WinAPI.au3> #include <GUIConstantsEx.au3> Global $g_hLabelStatus, $g_hMutex = 0, $g_iForkID = 0 ; Global Unique Windows Messages for IPC Global Const $g_idMsgGo = _WinAPI_RegisterWindowMessage("GameShow_GoSignal") Global Const $g_idMsgWin = _WinAPI_RegisterWindowMessage("GameShow_WinnerSignal") Global Const $g_idMsgBye = _WinAPI_RegisterWindowMessage("GameShow_GameOver") ; Unique Mutex Name for the Forks to fight over Global Const $g_sMutexName = "Local\GameShowBuzzerMutex" ; Determine mode based on command line arguments Switch ($CmdLine[0] > 0) ? $CmdLine[1] : "" Case "/Main" RunMain() Case "/Fork" RunFork(($CmdLine[0] > 1) ? $CmdLine[2] : 0) Case Else ; If run with no arguments, act as the launcher ConsoleWrite("Launcher: Starting Main. Bye." & @CRLF) ShellExecute(@AutoItExe, '"' & @ScriptFullPath & '" /Main') EndSwitch ; ========================================== ; Main loop ; ========================================== Func RunMain() ; Create Main Window to receive messages Local $hMainGUI = GUICreate("Game Main", 400, 200) Local $hLabel = GUICtrlCreateLabel("Spawning contestants...", 20, 40, 360, 100) GUICtrlSetFont(-1, 12, 800) GUISetState(@SW_SHOW) ; Allow lower-privilege (UIPI) messages on admin/user mix running forks ;~ _WinAPI_ChangeWindowMessageFilterEx($hMainGUI, $g_idMsgWin, 1) ; Register to listen for the Winner signal GUIRegisterMsg($g_idMsgWin, "Main_OnWinnerReceived") ; Spawn 3 Forks, passing their Fork ID (1, 2, 3) as an extra argument For $i = 1 To 3 ShellExecute(@AutoItExe, '"' & @ScriptFullPath & '" /Fork ' & $i) Sleep(200) ; Give them a brief moment to initialize their GUIs Next Sleep(1000) GUICtrlSetData($hLabel, "Ready... SET... GO!!!") ConsoleWrite("Main: Broadcasting GO signal!" & @CRLF) ; BROADCAST THE GO SIGNAL _WinAPI_BroadcastSystemMessage($g_idMsgGo, 0, 0, $BSF_POSTMESSAGE) ; Keep Main alive to await the winner Do Until GUIGetMsg() = $GUI_EVENT_CLOSE EndFunc ;==>RunMain Func Main_OnWinnerReceived($hWnd, $iMsg, $wParam, $lParam) ; $wParam contains the winning Fork's ID. MsgBox(64, "Main Decides!", "We have a winner! Fork #" & Int($wParam) & " hit the buzzer first!", 60, $hWnd) _WinAPI_BroadcastSystemMessage($g_idMsgBye, 0, 0, $BSF_POSTMESSAGE) ; tell the forks to exit. GUIDelete() Exit EndFunc ;==>Main_OnWinnerReceived ; ========================================== ; Fork loop ; ========================================== Func RunFork($iID) ; Create Fork Window to listen for Main's broadcast Local $hForkGUI = GUICreate("Fork " & $iID, 250, 150, 150 * $iID, 150 * $iID) $g_hLabelStatus = GUICtrlCreateLabel("Waiting for Main's GO...", 20, 50, 210, 50) GUICtrlSetFont(-1, 10, 600) GUISetState(@SW_SHOW) ; Allow UIPI bypass for the GO signal from Main ;~ _WinAPI_ChangeWindowMessageFilterEx($hMainGUI, $g_idMsgWin, 1) ; Store the ID globally so the message handler can access it $g_iForkID = $iID ; Listen for the GO signal GUIRegisterMsg($g_idMsgGo, "Fork_OnGoSignal") GUIRegisterMsg($g_idMsgBye, "Fork_GameOver") ; Register the clean-up function on exit OnAutoItExitRegister("Fork_CleanUp") ; ... wait ? Do Until GUIGetMsg() = $GUI_EVENT_CLOSE EndFunc ;==>RunFork Func Fork_OnGoSignal($hWnd, $iMsg, $wParam, $lParam) ; --- THE RACE IS ON! Try to create/lock the Mutex --- ; Open/Create a named mutex. If it already exists, @error or LastError will tell us. $g_hMutex = _WinAPI_CreateMutex($g_sMutexName, True) ; True = Attempt to take initial ownership ; If LastError is ERROR_ALREADY_EXISTS (183), some fork beat us to it! If _WinAPI_GetLastError() = 183 Then ; LOOSER! 😢 GUICtrlSetData($g_hLabelStatus, "LOOSER! 😭" & @CRLF & "Too slow!") _WinAPI_CloseHandle($g_hMutex) ; Clean up the handle Else ; WINNER! 🏆 GUICtrlSetData($g_hLabelStatus, "WINNER! 🏆" & @CRLF & "Slammed the buzzer first!") ; Broadcast back to the Main, passing the Fork ID in the wParam! _WinAPI_BroadcastSystemMessage($g_idMsgWin, $g_iForkID, 0, $BSF_POSTMESSAGE) EndIf EndFunc ;==>Fork_OnGoSignal Func Fork_CleanUp() ; If this instance holds an open Mutex handle, close it now! If IsDeclared("g_hMutex") And $g_hMutex <> 0 Then _WinAPI_CloseHandle($g_hMutex) ConsoleWrite("Fork: Mutex handle closed." & @CRLF) ; no one is gonna see it. meh. EndIf EndFunc ;==>Fork_CleanUp Func Fork_GameOver($hWnd, $iMsg, $wParam, $lParam) GUIDelete() Exit EndFunc ;==>Fork_GameOver Is it an example ?, is it a game ?, ...yes, is an example. They compete to be first to hit the buzzer ( is a mutex but buzzer sounds more fun ). Enjoy the game example
  2. Forking and threading. They both do stuff while another loop or event handler, does it's thing. Load a DLL, and ask for a function or procedure to run, and it'll do it. Handling all those takes time waiting, or to come up with semaphores and mutex and what not. So the issue at times is that we want to have all that in AutoIt and there comes the OMGs. To me, it all comes to run something and not get the main loop stuck waiting, unresponsive, as if frozen. ( oh, there is no threading in AutoIt, what can I do ! ) Hence this UDFish ( I'm not good at technical writing 😕 ), that has these functions: #cs === User Calltips: =============================================================================================== _Fork_Startup() Init. UDF - Place on your main script once everything is declared. _Fork_StartupOnFailMsgBox([$ShowMsgBox = Default]) Default = (not @compiled), True = Show error MsgBox(), False = Do Not show error MsgBox() _Fork_Func([$sFunction = Default], [$vParameter = ""], [$sExtraCmdLine = ""], [$iUseBase64Cmd = 0], [$sVerb = ""]) Starts another Process and Execute or Call $sFunction, Returns PID. _Fork_SetReceiver([$sFunction = ""]) Register/Unregister IPC Receiver Function. _Fork_GetReceiver() Get IPC Receiver Function name. _Fork_GetParentPID() Get the parent PID. _Fork_GetWinInfos([$AutoItWinTitlePrefix = Default],[ $ForkPID = ""]) Get an array of Forked Processes _Fork_CallArgArraySeparatorChar($sChar = Default) Default = Opt("GUIDataSeparatorChar") _Fork_AutoItWinTitlePrefix([$sPrefix = Default]) Returns the prefix, or set it by passing a new string. _Fork_ForkReceiverGuiTitlePrefix([$sPrefix = Default]) Returns the prefix, or set it by passing a new string. _Fork_ProcessGetWinList($vProcess, $sTitle = Default, $iOption = 0) Enumerates Windows of a Process. _Fork_WaitForReceiver($iPidChild, [$iTimeout = 60 Sec]) wait for _Fork_SetReceiver() to load via AutoItWinSetTitle(). _Fork_WaitForFork($iPidChild, [$iTimeout = 60 Sec]) wait for _Fork_Func() to load via AutoItWinSetTitle(). _Fork_Send($vPidOrHWnd, $sMessage,[$iTimeout = 500 mSec],[$fAbortIfHung = True]) Send IPC Message to Process via PID or hWnd. _Fork_Broadcast($sMessage, [$iTimeout = 500 mSec], [$fAbortIfHung = True], [$iDelayMs = 0], [$iExcludeSelf = 1], [$WinCloseAll = 0]) Send IPC Message broadcast to all in _Fork_AutoItWinTitlePrefix(), see comments. _Fork_DuplicateHandle($dwSourcePid, $hSourceHandle, $dwTargetPid = @AutoItPID, $fCloseSource = False) Returns a Duplicate handle. _Fork_GetVerUDF() Returns the version of this UDF. _DbgAid_SetActive([$Active = Default]) Enable sending data for debug: Default = Auto (True if Win found), True = Enable, False = Disable. _DbgAid_GetActive() Query Active state. _DbgAid_GuiTitle([$sTitle = Default]) Get or Set the GUI title for the debug receiver. _DbgAid_SelfName([$sSelfName = Default]) Get or Set a short name to be identified by. _DbgAid_Send($sString[, $iForceType = Default]) Send a string to the debug GUI. _DbgAid_SendVia([$iType = Default / $e_ForkDbgAid_ViaCOPYDATA / $e_ForkDbgAid_ViaMailslot]) Get / Set _DbgAid_Send() type. #ce === User Calltips: =============================================================================================== I believe that, with these functions, one can run a function in any count of other PIDs, communicate back and forth with any of them, and a console of sorts, to send data to follow what is happening with those other instances. This is basically a rewrite of "Another Multi Process Helper" by @piccaso. The functions not ported, I decided to not port. The renamed functions, are renamed to simplify the understanding of what they do, from a view of a ... me I did this because I'll need it in an upcoming project, and share it to aid those, that may find this, simple to implement in their code. Try the examples from SciTE ( or your editor ) and read the code, as there are notes explaining how it works. As always, share your views and improvements. If you have coding questions, kindly place them in "AutoIt General Help and Support". _Fork_UDF(v2019.06.29d).zip ( current version )
  3. The file is now at I added a WOL, tidy up, and moved the file to the uploads section of the forum. Suggestions are always mostly welcomed
×
×
  • Create New...