John Kens
John Kens

Reputation: 27

How to perform a proper loop

I'm trying to change a few addresses. However, I need the code to update them almost consistently. The code WORKS, BUT, when the checkbox is checked, it freezes and wont let me Unchecked it. (To stop changing the addresses)

Iv'e also tryed:

Loop While CheckBox1.CheckState = 1

But that does not help the issue.

Code:

Private Sub CheckBox1_Click(sender As Object, e As EventArgs) Handles CheckBox1.Click
    If IsProcessRunning("Notepad") = True Then
        Do
            Try
                WriteInteger("Notepad", &H49E6CC, 99)
                WriteInteger("Notepad", &H49E6D4, 99)
            Catch ex As Exception
            End Try
        Loop While True
    Else
        CheckBox1.CheckState = 0
        MessageBox.Show("Notepad Not Running!")
    End If
End Sub

Upvotes: 0

Views: 55

Answers (2)

John Kens
John Kens

Reputation: 27

[Problem Solved] Appreciate everyone help!

    Dim Timer1 As New Timer
Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
    If CheckBox1.CheckState = 1 Then
        AddHandler Timer1.Tick, AddressOf Timer1_Tick
        Timer1.Interval = 500
        Timer1.Start()

    Else
        RemoveHandler Timer1.Tick, AddressOf Timer1_Tick
        Timer1.Stop()
    End If
End Sub

Private Sub Timer1_Tick(sender As Object, e As EventArgs)
    WriteInteger("Notepad", &H49E6D4, 99)
    WriteInteger("Notepad", &H49E6CC, 99)
End Sub

Upvotes: 1

Jämes
Jämes

Reputation: 7245

The application becomes unresponsive when you click on the checkbox because you have written an infinite loop in the event handler. The main thread of the application handles the execution of the UI events.

You need to create a separated mechanism that is going to continuously executes your logic, such as a BackgroundWorker or using a separated thread.

Thus, the event handler of the check box only enables/disables the BackgroundWorker or the separated Thread.

Upvotes: 1

Related Questions