Buongiorno a tutti.
Sto cercando di risolvere questo problema di cross thread ma non riesco a capire perché non va.
codice:
<DllImport("user32.dll")> _
Private Shared Function GetForegroundWindow() As IntPtr
End Function
<DllImport("user32.dll")> _
Private Shared Function GetWindowThreadProcessId(hWnd As IntPtr, ProcessId As IntPtr) As IntPtr
End Function
<DllImport("user32.dll")> _
Private Shared Function AttachThreadInput(idAttach As IntPtr, idAttachTo As IntPtr, fAttach As Boolean) As IntPtr
End Function
<DllImport("user32.dll")> _
Private Shared Function GetFocus() As IntPtr
End Function
Public Function HwndActiveWindow() As String
If Me.InvokeRequired Then
Me.Invoke(New MethodInvoker(AddressOf HwndActiveWindow))
Else
Dim activeWindowHandle As IntPtr = GetForegroundWindow()
Dim activeWindowThread As IntPtr = GetWindowThreadProcessId(activeWindowHandle, IntPtr.Zero)
Dim thisWindowThread As IntPtr = GetWindowThreadProcessId(Me.Handle, IntPtr.Zero)
AttachThreadInput(activeWindowThread, thisWindowThread, True)
AttachThreadInput(activeWindowThread, thisWindowThread, False)
Dim focusedControlHandle As IntPtr = GetFocus()
Return focusedControlHandle.ToString
End If
End Function
Praticamente invokerequired è true ma invece di ritornare alla funzione mi salta a endif
Mi aiutate a risolvere ?
Grazie, Alberto