Waiting until certain condition is met to continue executing the code


New member
Mar 9, 2015
Programming Experience
Hello all,

I am brand new to the forums and if I posted this in the wrong section I apologize (please direct me to where it is supposed to be posted). Anyways, I am somewhat new to coding and am looking for some help executing a certain thing in my code.

The back story is that I created a simple program that automates some of the tasks that we have to do online. A simpler way of saying it is that literally all it is, is an auto clicker that performs the task. Right now, I have sleep functions all over my program (and it works fine but for obvious reasons that an action may not complete in the set time I put it to sleep it is not quite 100% safe).

For example I set it to click a certain thing, and sometimes it takes 2 seconds, sometimes 3, sometimes 10 to load the form or open a page. Which is fine, I just put the sleep to do 15 seconds and it will work... But I want to make it more efficient and safer so that in case it somehow does not load the page in 10 seconds it waits longer or perhaps repeats the last function because it might of screwed up somewhere.

So I started playing around with screen pixels. This is the code that I have right now.

Pixel Program.png

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        Dim a As New Drawing.Bitmap(1, 1)
        Dim b As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(a)
        b.CopyFromScreen(New Drawing.Point(MousePosition.X, MousePosition.Y), New Drawing.Point(0, 0), a.Size)
        Dim c As Drawing.Color = a.GetPixel(0, 0)
        PictureBox1.BackColor = c
        TextBox1.Text = PictureBox1.BackColor.ToString
    End Sub

I added a second text box and a button just for testing purposes (the previous code would not allow me to create an if statement to check multiple pixels). So the code with the second text box and button function is below.


 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim screenSize As Size = New Size(My.Computer.Screen.Bounds.Width, My.Computer.Screen.Bounds.Height)
        Dim screenGrab As New Bitmap(My.Computer.Screen.Bounds.Width, My.Computer.Screen.Bounds.Height)
        Dim g As Graphics = Graphics.FromImage(screenGrab)
        g.CopyFromScreen(New Point(0, 0), New Point(0, 0), screenSize)
        Dim pc As Color = screenGrab.GetPixel(56, 91)
        TextBox2.Text = pc.ToString

And with that I can also create an If statement.

If (screenGrab.GetPixel(56, 91).ToString = "Color [A=255, R=251, G=106, B=184]" And screenGrab.GetPixel(161, 272).ToString = "Color [A=255, R=6, G=122, B=233]" And screenGrab.GetPixel(197, 198).ToString = "Color [A=255, R=254, G=15, B=123]") Then
            MessageBox.Show("Pixel are correct colors.")
            MessageBox.Show("Pixels are not correct colors")
        End If

NOW........ What I want to do is make the program wait UNTIL a certain pixel or multiple pixels are a certain color. For example

Wait until screenGrab.GetPixel(56, 91).ToString = "Color [A=255, R=251, G=106, B=184]" and then continue executing code.

Lets say.

Click here

Click there

Click here

Click there

Wait until pixel is right color to continue

Click there

Click here

Currently I have a Do Loop thing going on

Dim testColor As Color = Color.FromARGB(255, 251, 106, 184)
Loop Until screenGrab.GetPixel(56, 91) = testColor

MessageBox.Show("Pixel located")

This works fine but ONLY if that pixel with the color is on the screen ALREADY when the button is hit. If the pixel is not already on the screen the program just freezes up. I need for the program to just wait until that pixel shows up on the screen and then continue executing the rest of the code.

I hope I made any sense here. If I am confusing as hell please let me know to explain better!

Thank you all,

Filip K.
Top Bottom