Delay without timer or threading.sleep

mcdonger

Member
Joined
Mar 18, 2007
Messages
16
Location
England
Programming Experience
1-3
I am working on a macro to interact with an online game. It needs short delays between clicks etc. I have got the macro working, using

System.Threading.Thread.Sleep(200)

PHP:
        While x < repeats

           SetCursorPos(xbase + 255, ybase + 167)
            System.Threading.Thread.Sleep(200)
            rightclick()
            System.Threading.Thread.Sleep(200)
and so forth...


However I need a way to stop this macro running at any point, using a hotkey. Perhaps there is a better way to do this also, but I have used

PHP:
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Integer) As Integer
And when (F8 I have used) is pressed, the loop ends.

The problem is that by using system.threading.thread.sleep, the whole application is effectively paused, and so the timer on which the GetAsyncKeyState function is running, cannot check for a keypress. When the macro is not running, it reads the key perfectly.

Is there any way of putting a delay in without pausing the whole application, or another way i might be able to stop the macro running?*

*I have a button on the application, and it is set to alwaysontop, however becuase the macro is clicking up to 7 times per second, i cannot give the application focus.

Thankyou in advance
 
Run the routine as a different thread and abort it from the F8 keypress.
VB.NET:
Private t As New Threading.Thread(AddressOf DifferentThread)
 
Private Sub StartThread()
    t.Start()
End Sub
 
Private Sub DifferentThread()
    ' do stuff
End Sub
 
Private Sub frm_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
    If e.KeyCode = Keys.F8 Then t.Abort()
End Sub
 
Back
Top