如何知道剪贴板上的数据已经可以使用了?

How to know that the data on the clipboard is ready to be used?

我的代码复制一个 table,处理复制的数据,复制另一个 table,处理复制的数据等等...

问题是有时候有些table的数据很多,需要很长时间才能复制,导致try/catch块出错。

有没有办法在剪贴板上的数据准备好后才开始处理复制的数据?

   Private Sub Copy
      Clipboard.Clear()
            Try
                Win32.SetCursorPos(200,200)
                Win32.mouse_event(Win32.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
                Win32.mouse_event(Win32.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
                SendKeys.SendWait("^C")
            Catch ex As Exception
                Msgbox (ex.message)
            End Try


            'PROCESS THE COPIED DATA
        Try
            Dim clip() As String
            Dim col() As String
            clip = Clipboard.GetText().Split(Environment.NewLine)
            col = clip(i).Split(vbTab)
            'I test the data to see if it interests me
        Catch ex As Exception

        End Try


     Clipboard.Clear()
            Try
                Win32.SetCursorPos(600,200)
                Win32.mouse_event(Win32.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
                Win32.mouse_event(Win32.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
                SendKeys.SendWait("^C")
            Catch ex As Exception
                Msgbox (ex.message)
            End Try



            'PROCESS THE COPIED DATA


     Clipboard.Clear()
            Try
                Win32.SetCursorPos(400,200)
                Win32.mouse_event(Win32.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
                Win32.mouse_event(Win32.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
                SendKeys.SendWait("^C")
            Catch ex As Exception
                Msgbox (ex.message)
            End Try


            'PROCESS THE COPIED DATA


    End Sub

根据@jmcilhinney 的评论,这是一个循环。没有GoTo。在 If ErrorCount > 3 Then 中设置尝试次数。你在重复你的代码。通常不是维护的好主意。唯一改变的是左侧和顶部,所以我将它们作为参数传递。消息框的显示将提供暂停。您的 ProcessCopiedData 可能应该是从 Copy 子调用的单独子。

Private Sub Copy(left As Integer, top As Integer)
    Dim ErrorCount As Integer
    Do
        Clipboard.Clear()
        Try
            Win32.SetCursorPos(left, top)
            Win32.mouse_event(Win32.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
            Win32.mouse_event(Win32.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
            SendKeys.SendWait("^C")
            ErrorCount = 0
        Catch ex As Exception
            ErrorCount += 1
            MsgBox(ex.Message)
        End Try
        If ErrorCount > 3 Then
            MessageBox.Show("Exceeded # of Trys.")
            Exit Sub
        End If
    Loop Until ErrorCount < 1
    'Process Copied Data
End Sub

用法...

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Copy(200, 200)
    Copy(600, 200)
    Copy(400, 200)
End Sub