Jump to content

Blocking ClipPut() ?


Just1f
 Share

Recommended Posts

When you uses Clipget() you get clipboard buffer as result, and the script holds on until ClipGet() returns.

But when you uses ClipPut($data) it sends the data to the clipboard but Autoit does not verify that Windows finished to copy the text to the clipboard before continuing the script.

That means that you can have a race condition where your ClipGet() will not sometimes get what you previously set in ClipPut() if there is a short delay between them.

How to have ClipPut() blocks the scripts until being sure Windows finished to transfer the data to the clipboard ?

Thank you.

Edited by Just1f
Link to comment
Share on other sites

Not to sure on this one, but maybe this example could help?

#include <GuiConstantsEx.au3>
#include <ClipBoard.au3>
#include <WinAPI.au3>

Opt('MustDeclareVars', 1)

Global $iMemo

_Main()

Func _Main()
    Local $hGUI, $btn_SetData, $btn_GetData, $hMemory, $hLock, $tData, $sData, $iSize

   ; Create GUI
    $hGUI = GUICreate("Clipboard", 600, 450)
    $iMemo = GUICtrlCreateEdit("", 2, 2, 596, 396, $WS_VSCROLL)
    GUICtrlSetFont($iMemo, 9, 400, 0, "Courier New")
    $btn_SetData = GUICtrlCreateButton("Set ClipBoard Data", 150, 410, 120, 30)
    $btn_GetData = GUICtrlCreateButton("Get ClipBoard Data", 300, 410, 120, 30)
    GUISetState()

   ; Loop until user exits
    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop
            Case $btn_SetData
               ; Open clipboard
                If Not _ClipBoard_Open (0) Then _WinAPI_ShowError ("_ClipBoard_Open failed")

               ; Empty clipboard
                If Not _ClipBoard_Empty () Then _WinAPI_ShowError ("_ClipBoard_Empty failed")

               ; Create global memory buffer (show why using _ClipBoard_SetData is MUCH easier!)
                $sData = "Hello from AutoIt"
                $iSize = StringLen($sData) + 1
                $hMemory = _MemGlobalAlloc ($iSize, $GHND)
                If $hMemory = 0 Then _WinAPI_ShowError ("_Mem_GlobalAlloc failed")
                $hLock = _MemGlobalLock ($hMemory)
                If $hLock = 0 Then _WinAPI_ShowError ("_Mem_GlobalLock failed")
                $tData = DllStructCreate("char Text[" & $iSize & "]", $hLock)
                DllStructSetData($tData, "Text", $sData)
                _MemGlobalUnlock ($hMemory)

               ; Write clipboard text
                If Not _ClipBoard_SetDataEx ($hMemory, $CF_TEXT) Then _WinAPI_ShowError ("_ClipBoard_SetDataEx failed")

               ; Close clipboard
                _ClipBoard_Close ()
            Case $btn_GetData
                MemoWrite(_ClipBoard_GetData ())
        EndSwitch
    WEnd

EndFunc  ;==>_Main

; Write message to memo
Func MemoWrite($sMessage = "")
    GUICtrlSetData($iMemo, $sMessage & @CRLF, 1)
EndFunc  ;==>MemoWrite
Link to comment
Share on other sites

How to have ClipPut() blocks the scripts until being sure Windows finished to transfer the data to the clipboard ?

This seems to work as it checks against the contents of the clipboard until it is a match with what ClipPut() added.

; example to test
For $i = 1 To 100
    _ClipPut("some text " & $i)
Next

Exit

Func _ClipPut($text)
    ClipPut($text)
    Do
        Sleep(10)
    Until ClipGet() = $text
    ConsoleWrite($text & @CRLF); test output
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...