Why won't my Loop code be?

Sorry for the messy code: S

If CheckBox2.Checked = True Then
For i As Integer = 0 To 1 Step 0
If CheckBox1.Checked = True Then
        If TextBox1.Text = lblCLickLImit.Text Then
            Timer1.Stop()
            TextBox1.Text = "0"
            System.Windows.Forms.SendKeys.Send("{F5}")
            System.Threading.Thread.Sleep(delaydelaytime)
            System.Windows.Forms.SendKeys.Send("{ENTER}")

        Else
            If CheckBox1.Checked = False Then
                If TextBox1.Text = lblCLickLImit.Text Then
                    Timer1.Stop()
                    TextBox1.Text = "0"
                End If
            End If
        End If
    Else
        If CheckBox2.Checked = False Then
            If CheckBox1.Checked Then
                If TextBox1.Text = lblCLickLImit.Text Then
                    Timer1.Stop()
                    TextBox1.Text = "0"
                    System.Windows.Forms.SendKeys.Send("{F5}")
                    System.Threading.Thread.Sleep(delaydelaytime)
                    System.Windows.Forms.SendKeys.Send("{ENTER}")
                End If
            Else
                If CheckBox1.Checked = False Then
                    If TextBox1.Text = lblCLickLImit.Text Then
                        Timer1.Stop()
                        TextBox1.Text = "0"
                    End If
                End If
            End If
        End If
    End If
Next

      

Basically this code is for the Auto Clicker program (hopefully this helps you understand the http://prntscr.com/7tuc3o interface) So when "Continuous" then it is theoretically assumed that the code should be closed for infinity. However, when I run the program with everything selected as shown, all that happens is that the program clicks once and then crashes (not responding). Any help I've tried this loop in other programs and it works, just not with this code.

+3


source to share


3 answers


Your loop is tying the UI thread. You will need to look into using a background worker:

BackgroundWorker handles long running tasks. This will not slow down the entire program as it completes this task. ( dotnetperls.com )

Here is a msdn walkthrough on setting up a background worker: https://msdn.microsoft.com/en-us/library/ywkkz4s1.aspx



Or

If this is a personal project and that nobody likes, you will need to maintain this code , you can use Application.DoEvents()

to continue pumping messages during a loop. Here is the msdn documentation for https://msdn.microsoft.com/en-us/library/system.windows.forms.application.doevents(v=vs.110).aspx

+1


source


First of all, a Step

of 0 really doesn't make any sense in a loop for

. It may / work /, but it will make someone read it insane later. If you want an infinite loop not to use a loop for

, use:

While True
    'code here
End While

      



Cycles

for

are used when you know exactly how many iterations of your loop you need. The loop is while

meant to repeat as long as a condition is met. In this example, the condition is always true, so it loops infinitely.

Your code will also keep spinning the UI thread. It never pauses for input (even your sleep calls don't free the thread for input). As far as the OS knows, your application is locked because it never processes any of the window messages that will be sent to it. However, he still spins happily until the windows finally get tired of him and ask you to kill him.

0


source


Not sure what other "programs" you are running, but I can tell you that you don't want to use a For loop for this. You need a do / while loop, for example:

While True
   ...
End While

      

or

Do
   ...
Loop While True

      

-3


source







All Articles