I want to press a Button of an external application to close it (I MUST press the Button to confirm some changes)
Code:
some code before.....
Cursor.Position = New Point(X,Y)
NativeMethods.Mouse_Event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, IntPtr.Zero)
NativeMethods.Mouse_Event(MOUSEEVENTF_LEFTUP, 0, 0, 0, IntPtr.Zero)
'test
Threading.Thread.Sleep(5000)
The question is: why if I insert a Sleep(5000) event the button IS NOT pressed until the end of the Sleep event? When I reach the Sleep code the MouseEvent code has already been processed
How to force anyway the MouseEvent?
In effect the true code is:
Code:
Cursor.Position = New Point(X,Y)
NativeMethods.Mouse_Event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, IntPtr.Zero)
NativeMethods.Mouse_Event(MOUSEEVENTF_LEFTUP, 0, 0, 0, IntPtr.Zero)
Threading.Thread.Sleep(100)
'I want to be sure the window is closed...
If NativeMethods.FindWindow(Nothing, WinHwnd) <> IntPtr.Zero Then
Cursor.Position = New Point(X,Y)
NativeMethods.Mouse_Event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, IntPtr.Zero)
NativeMethods.Mouse_Event(MOUSEEVENTF_LEFTUP, 0, 0, 0, IntPtr.Zero)
But since the MouseEvent don't works until the End of the Sub the second part of the code is ALWAYS processed, and I am not sure that the window will be really closed.
All the code (that is quite complex) is in a Module, I ca'nt add Timers or Background Workers...
The more simple way would be to force the MouseEvent BEFORE to exit from the Sub