C # delay time per application
I am currently developing an application, I would like to implement a time delay. I don't want to use System.Threading.Thread.Sleep (x); how i read this kiosk thread (ui hangs)
The code I have currently written:
public void atimerdelay()
{
lblStat.Text = "In the timer";
System.Timers.Timer timedelay;
timedelay = new System.Timers.Timer(5000);
timedelay.Enabled = true;
timedelay.Start();
}
I know that atimerdelay () makes the call (using lblStat), but there is no delay of 5 seconds. I have read http://msdn.microsoft.com/en-us/library/system.timers.timer.aspx but I cannot figure out why the above code is not working.
Additional information - (added for Hamlet Hakobyan)
The application is a card verification application (university project - no real information is used or stored). After the user has filled in all the relevant data and clicked "check", a list of check methods is called. Before any of these are called ping is done to make sure the computer is connected to the internet. I want to add a little delay between ping and the start of the check.
source to share
I will never forget this wonderful piece . It is suitable for Windows Form applications and is much more lightweight than creating Timer
.
source to share
After looking for a delay function (not using Sleep ) I found this
public static DateTime PauseForMilliSeconds(int MilliSecondsToPauseFor)
{
System.DateTime ThisMoment = System.DateTime.Now;
System.TimeSpan duration = new System.TimeSpan(0, 0, 0, 0, MilliSecondsToPauseFor);
System.DateTime AfterWards = ThisMoment.Add(duration);
while (AfterWards >= ThisMoment)
{
System.Windows.Forms.Application.DoEvents();
ThisMoment = System.DateTime.Now;
}
return System.DateTime.Now;
}
It works great, tested with .NET 4.0
. Maybe it might be helpful for someone else.
source to share
After the user fills in all the required information and click "check" the list of validation methods. Before any of these are pinged to make sure the internet connection is included in the computer. I want to add a small delay between the ping and the start of the check.
The good thing is that all your long-running tasks are done in a separate thread. If you have one .NET 4.0+
, you can use Task
( TPL ). If your tasks interact with UI
, you can use the Event Based Asynchronous Pattern if not TPL
.
Specifically about Ping
you can use Ping.SendAsync
. For details see.
Try this and come back with new, more interesting questions.
source to share