Small Delay in code, WITHOUT timer object
Kinda new to C# but dont alot of programming in java and vb and many other languages...
i dont want to use a timer object, i am using visual studio...
i would like to make just like a 10 second delay in the code and im stuck lol
Scott LyslePosted Nov 18, 2006, 4:08 PM
add a reference: using System.Threading;
then, where you want your 10 second delay, call: Thread.Sleep(10000);
Example, takes a single form with two labels and a button, set the button event handler to this:
private
void button1_Click(object sender, EventArgs e){
label1.Text = DateTime.Now.Second.ToString();
label2.Text = "0";
label1.Refresh();
label2.Refresh();
Thread.Sleep(10000);
label2.Text = DateTime.Now.Second.ToString();
}
If precision is an issue, you may want to consider using an API built timer. Also, Environment.TickCount returns the time since the last system start in milliseconds and you could use that to build a delay (set a start time and offset and check the current tickcount value against the offset to determine how much time has passed). You could also do something like that with the DateTime.Now.Seconds value (that is, get a start point and and a target value and poll for the current value reaching or passing the target value).