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.

+3


source to share


5 answers


What about:

await Task.Delay(5000)

      



? It won't block the UI thread and looks pretty good!

+7


source


I will never forget this wonderful piece . It is suitable for Windows Form applications and is much more lightweight than creating Timer

.



+2


source


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.

+1


source


You can use System.Timers.Timer

to trigger an event after a lapse of time. They are not useful for simply deferring execution.

If you added: timedelay.Elapsed += delayedFunction;

to your code then delayedFunction

gets called after 5 seconds.

0


source


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.

0


source







All Articles