How can I not fall in "not responding " while doing long loop?

Hi .. I'm a student that I helped by you when I was learning c for unix..

I want to ask you about different thing this time and I know you can help me
be cause I like your style helping people..
I want to ask about .net c# ...

I did a simple program that make me control my windows system using c# like turning it off or lock it at certain time the user input.. it's like scheduler but a lot easier to use my problem is "sorry for the messy desktop"

that my program will inter in a while loop until the system time = the input time.. and that cause a not responding and that is not efficient.. I tried to add "sleep" but that didn't work with it and still not responding on the while loop
any solutions to make it run normally.. this is a function of the code.."the others are same but a little different"

private void button1_Click(object sender, EventArgs e)
        {
            string clock;
            clock = textBox1.Text;
            System.Windows.Forms.MessageBox.Show("system will close in " + clock);
            if (radioButton1.Checked)
            {
                clock = clock + " AM";
            }
            if (radioButton2.Checked)
            {
                clock = clock + " PM";
            }
            while (DateTime.Now.ToString("h:mm tt") != clock) // here is the problem
            {
                if (DateTime.Now.ToString("h:mm tt") == clock)
                    break;
               

            }
            
            System.Diagnostics.Process.Start("shutdown", "/s /t 0");
            this.Close();
        }

Where did you try the sleep? One time test >= is better, in case you sleep past.

1 Like

i think using timer control and timer_tick event would be better. Google it.

1 Like

However, it sounds like your routine call might have been in a interrupts masked state, so you may need to hand off alarming to another thread and return to get interrupts serviced.