Wait 20 Seconds In Timer Before Executing Next Line Without Thread.Sleep.C# -
i'm trying wait 20 seconds before adding + 1 value (int), want without thread.sleep
.
this code, way i'm not pro programmer.
private void refresh_app_timernh_tick(object sender, eventargs e) { label18.text = "timer activated"; int = 0; = + 1; if (i == 16) { = 0; } else { } if (i == 1) { webbrowser1.refresh(); useridlabel1.backcolor = color.red; label20.text = "+1"; //**i want add 20 second gap here** = + 1; } else { } if (i == 2) { webbrowser2.refresh(); useridlabel2.backcolor = color.red; label20.text = "+2"; = + 1; } else { } if (i == 3) { webbrowser3.refresh(); useridlabel3.backcolor = color.red; label20.text = "+3"; = + 1; } else { } if (i == 4) { webbrowser4.refresh(); useridlabel4.backcolor = color.red; label20.text = "+4"; = + 1; } else { } if (i == 5) { webbrowser5.refresh(); useridlabel5.backcolor = color.red; label20.text = "+5"; = + 1; } else { } if (i == 6) { webbrowser6.refresh(); useridlabel6.backcolor = color.red; label20.text = "+6"; = + 1; } else { } if (i == 7) { webbrowser7.refresh(); useridlabel7.backcolor = color.red; label20.text = "+7"; = + 1; } else { } if (i == 8) { webbrowser8.refresh(); useridlabel8.backcolor = color.red; label20.text = "+8"; = + 1; } else { } if (i == 9) { webbrowser9.refresh(); useridlabel9.backcolor = color.red; label20.text = "+9"; = + 1; } else { } if (i == 10) { webbrowser10.refresh(); useridlabel10.backcolor = color.red; label20.text = "+10"; = + 1; } else { } if (i == 11) { webbrowser11.refresh(); useridlabel11.backcolor = color.red; label20.text = "+11"; = + 1; } else { } if (i == 12) { webbrowser12.refresh(); useridlabel12.backcolor = color.red; label20.text = "+12"; = + 1; } else { } if (i == 13) { webbrowser13.refresh(); useridlabel13.backcolor = color.red; label20.text = "+13"; = + 1; } else { } if (i == 14) { webbrowser14.refresh(); useridlabel14.backcolor = color.red; label20.text = "+14"; = + 1; } else { } if (i == 15) { webbrowser15.refresh(); useridlabel15.backcolor = color.red; label20.text = "+15"; = + 1; } else { } if (i == 16) { webbrowser16.refresh(); useridlabel16.backcolor = color.red; label20.text = "+16"; = + 1; } else { } refresh_app_timernh.stop(); label18.text = "timer de-activated"; refresh_app_timernh.start(); }
i think might easy not me, because i'm new c#
first off, tim s's answer -- break logic smaller chunks , simplify timer logic -- good. answer specific question, "how delay between statements without sleep
", is: make method async
, use await task.delay(...)
.
you right avoid sleep
; using bad programming practice, , hang application. await task.delay
asynchronous wait -- is, method returns immediately, app keeps on processing ui messages, , when delay done, program schedules remainder of method execute later.
note during asynchronous wait, design messages keep on getting processed. if 1 of messages causes event handler run again can confusing situation of having multiple control points in same non-recursive method. try avoid that.
Comments
Post a Comment