Set application in idle state

2 posts in this topic

Hi guys

I have a script that runs for about 20 seconds.

If Outlook.exe is not already open, and I open it while the script is running, I want Outlook to wait until the script is done, and then open.
Is it possible to set an application in some sort of idle state until the script is done? I want to avoid it from just closing Outlook, and open it again.



Share this post

Link to post
Share on other sites

; Johnmcloud - 2016
; _GetAllProcessThreads by monoceres, a little edited

Local $iPID = Run("notepad")


Func _ProcessSuspend($iPID)
    Local $THREAD_SUSPEND_RESUME = 0x0002
    Local $hWnd, $aProcessThreads = _GetAllProcessThreads($iPID)
    For $i = 1 To $aProcessThreads[0][0]
        $hWnd = DllCall('Kernel32.dll', 'hwnd', "OpenThread", "int", $THREAD_SUSPEND_RESUME, "int", 0, 'int', $aProcessThreads[$i][0])
        If Not @error Then DllCall('Kernel32.dll', 'uint', "SuspendThread", 'hwnd', $hWnd[0])
    Return 1
EndFunc   ;==>_ProcessSuspend

Func _ProcessResume($iPID)
    Local $THREAD_SUSPEND_RESUME = 0x0002
    Local $hWnd, $aProcessThreads = _GetAllProcessThreads($iPID)
    For $i = 1 To $aProcessThreads[0][0]
        $hWnd = DllCall('Kernel32.dll', 'hwnd', "OpenThread", "int", $THREAD_SUSPEND_RESUME, "int", 0, 'int', $aProcessThreads[$i][0])
        If Not @error Then DllCall('Kernel32.dll', 'uint', "ResumeThread", 'hwnd', $hWnd[0])
    Return 1
EndFunc   ;==>_ProcessResume

Func _GetAllProcessThreads($iPID)
    Local Const $TH32CS_SNAPTHREAD = 0x00000004
    Local Const $THREADENTRY32 = "dword dwSize;dword cntUsage;dword th32ThreadId;dword th32OwnerProcessID;long tpBasePri;long tpDeltaPri;dword dwFlags;"
    Local $aCall = DllCall("Kernel32.dll", "ptr", "CreateToolhelp32Snapshot", "dword", $TH32CS_SNAPTHREAD, "dword", 0)
    If @error Then Return SetError(1, 0, 0)
    Local $hThread = $aCall[0]
    Local $aReturn[1][3]
    Local $Thread32 = DllStructCreate($THREADENTRY32)
    DllStructSetData($Thread32, "dwSize", DllStructGetSize($Thread32))
    $aCall = DllCall("Kernel32.dll", "int", "Thread32First", "ptr", $hThread, "ptr", DllStructGetPtr($Thread32))
    If @error Then Return SetError(2, 0, 0)
    If DllStructGetData($Thread32, "th32OwnerProcessID") = $iPID Then _GetAllThreads_ArrHelper($aReturn, $Thread32)
        $aCall = DllCall("Kernel32.dll", "int", "Thread32Next", "ptr", $hThread, "ptr", DllStructGetPtr($Thread32))
        If Not $aCall[0] Then ExitLoop
        If DllStructGetData($Thread32, "th32OwnerProcessID") = $iPID Then _GetAllThreads_ArrHelper($aReturn, $Thread32)
    Until True And False
    $aReturn[0][0] = UBound($aReturn) - 1 ; number of items in the array
    DllCall("Kernel32.dll", "bool", "CloseHandle", "handle", $hThread)
    If @error Then Return SetError(3, 0, 0)
    Return $aReturn
EndFunc   ;==>_GetAllProcessThreads

Func _GetAllThreads_ArrHelper(ByRef $aArray, $Thread32_Struct)
    Local $iUBound = UBound($aArray)
    ReDim $aArray[$iUBound + 1][3]
    $aArray[$iUBound][0] = DllStructGetData($Thread32_Struct, "th32ThreadId")
    $aArray[$iUBound][1] = DllStructGetData($Thread32_Struct, "th32OwnerProcessID")
    $aArray[$iUBound][2] = DllStructGetData($Thread32_Struct, "tpBasePri")
EndFunc   ;==>_GetAllThreads_ArrHelper


Share this post

Link to post
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

  • Similar Content

    • ur
      By ur
      with the below code, I am keeping waiting for a particular file and waiting in the background.
      While $i <> 10 Sleep(60000) If FileExists(@ScriptDir&"\Binaries.ini") then --some processing-- $i = 10 endif WEnd I thought keeping the Sleep will freeze the process from resources but the CPU being uilized.But it is taking 47%.
      How to free this CPU usage also.?

    • ur
      By ur
      Is there anyway to check whether the script is launched already and wait till the other instance is closed and continue after that.
    • leomoon
      By leomoon
      I'm trying to write a hotkey program and I know exactly how to do it but it will be hard coded and the user won't be able to edit it.
      HotKeySet("{ESC}", "HotKeyPressed") HotKeySet("^!d", "HotKeyPressed") HotKeySet("^+x", "HotKeyPressed") While 1 Sleep(500) WEnd Func HotKeyPressed() Switch @HotKeyPressed Case "{ESC}" Exit Case "^!d" ShellExecute("D:\Docs") Case "^+x" ShellExecute("D:\Docs\My Portables\Burner\AnyBurn\AnyBurn.exe") EndSwitch EndFunc I want the program to read a file that has hotkeys on odd lines and paths on even lines but I don't see how the program will load into itself.
      Is it even possible?
    • ur
      By ur
      With  ProcessList ( ["name"] )  we are able to get the running process list from the local machine.
      Is there anyway to get the list from Remote machine, more precisely to get to know the status whether a particular application is running or not on remote machine using AutoIT?
      We can implement through PSList.exe, but again we need to parse the text of it to read the output.
      Is there any direct UDF in AutoIT?
    • Nareshm
      By Nareshm
      If Process exits then end process and ;Some code here {1}
      If Process does not exits then ; My {1} Code