Results 1 to 3 of 3

Thread: [RESOLVED] Waking thread before thread sleep time finishes

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2020
    Posts
    23

    Resolved [RESOLVED] Waking thread before thread sleep time finishes

    Hi everyone
    I'm writing an auto clicker to do a repetitive task which needs a specific time before repeating the macro
    I built it but I ran through a problem
    sometimes I need to stop the macro while it's in the
    Code:
    Thread.Sleep(140000)
    part of the code
    I tried using a combination of do untils and timers and counters but it seems that
    Code:
    Thread.Sleep(140000)
    affects this too.
    I tried googling for a solution but all the pages I found were excel related.
    How can i fix this ?
    Here's the code :
    Code:
    If GetAsyncKeyState(Keys.F9) Then
                    Do Until GetAsyncKeyState(Keys.F8)
                        For Each line As String In File.ReadLines(Application.StartupPath & "\Macros\" & ComboBox1.Text)
                            If line.Length > 2 Then
                                Dim g = Regex.Replace(line, "[\{\}a-zA-Z=()]", "").Split(","c)
                                Dim pointResult As Point = New Point(Integer.Parse(g(0)), Integer.Parse(g(1)))
                                System.Windows.Forms.Cursor.Position = pointResult
                                Thread.Sleep(2500)
                                LeftClick()
                                If GetAsyncKeyState(Keys.F8) Then
                                    Macro.Enabled = False
                                    Macro.Stop()
                                    N = 0
                                    Exit Do
                                End If
                            Else
                                Thread.Sleep(1000)
                            End If
                        Next
                        Thread.Sleep(140000) 'here where i want to stop the thread.sleep by keybord input for ex 'F8
    
                    Loop
                End If
    thanks

  2. #2

    Thread Starter
    Junior Member
    Join Date
    Jan 2020
    Posts
    23

    Re: Waking thread before thread sleep time finishes

    Hi
    i managed to fix it by using a a do until function
    here's the working code in case someone needs it in the future :

    Code:
                If GetAsyncKeyState(Keys.F9) Then
                    stopsignal = False
                    waitperiod = 0
                    Do Until GetAsyncKeyState(Keys.F8) Or stopsignal = True
    
                        For Each line As String In File.ReadLines(Application.StartupPath & "\Macros\" & ComboBox1.Text)
                            If line.Length > 2 Then
                                Dim g = Regex.Replace(line, "[\{\}a-zA-Z=()]", "").Split(","c)
                                Dim pointResult As Point = New Point(Integer.Parse(g(0)), Integer.Parse(g(1)))
                                System.Windows.Forms.Cursor.Position = pointResult
                                Thread.Sleep(1500)
                                LeftClick()
                                If GetAsyncKeyState(Keys.F8) Then
                                    Macro.Enabled = False
                                    Macro.Stop()
                                    N = 0
                                    Exit For
                                End If
                            Else
                                Thread.Sleep(1000)
                            End If
                        Next
                        ExitBo = True
                        waitperiod = 0
                        Do Until ExitBo = False
                            Thread.Sleep(1000)
                            waitperiod += 1
                            If waitperiod = 120 Then
                                ExitBo = False
                            End If
                            If GetAsyncKeyState(Keys.F8) Then
                                Macro.Enabled = False
                                Macro.Stop()
                                stopsignal = True
                                Exit Do
                            End If
                        Loop
    
                    Loop
                End If

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: [RESOLVED] Waking thread before thread sleep time finishes

    That's the way to do it.

    Sleep is a very efficient method. It's not partially awake, it's fully asleep, so the only way to interrupt is to sleep for shorter segments of time and check when you awake. You can go for shorter intervals than you have and still be quite efficient, but the longer you sleep, the less work the CPU does, in general.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width