My app detect is ip changed or not. If ip is different than previous it saves new ip to file. Seems pretty easy.
Delay function:
public void MyDelay(int milliseconds)
{
    Stopwatch stopwatch = Stopwatch.StartNew();
    while (stopwatch.ElapsedMilliseconds <= (long)milliseconds)
    {
        Application.DoEvents();
    }
}
Button click event:
private void button1_Click(object sender, EventArgs e)
  {
    pictureBox1.BackColor = Color.Green;
    while (true)
    {
        while (true)    //force obtain new_ip
        {
            try
            {
                new_ip = new System.Net.WebClient().DownloadString("https://api.ipify.org");
            }
            catch
            {
                MyDelay(1000);
                continue;
            }
            break;
        }
        if (old_ip != new_ip)
        {
            pictureBox1.BackColor = Color.Red;
            File.AppendAllText(@"C:\users\pc\Desktop\ip.txt", new_ip + "\r\n");
            old_ip = new_ip;
            MessageBox.Show("New ip saved");
        }
        pictureBox1.BackColor = Color.Green;
    }
 }
The thing is if I left the code like this the part: pictureBox1.BackColor = Color.Green; is not going to execute.
I have to change from this:
pictureBox1.BackColor = Color.Green;
To this:
pictureBox1.BackColor = Color.Green;
MyDelay(1);
And now picturebox colour is changing. Why does that Delay repair that? If I put MessageBox insted of Picturebox, problem does not even exist. It work as a charm without MyDelay too. So what is the point?
Another question: it should check the ip every 1 second. So why when I change the ip I have to wait about 5 second for change detection? It should save new ip after 1 second when connection appears, but it takes about 5 second to recognize change
 
     
     
     
     
    