Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What should I use Sleep or Timer

Tags:

c#

sleep

timer

I have two alternative using timer or using sleep, I need to call a method every 3 seconds after this method is finished, I wrote basic example to demonstrate what I mean:

public static void Main()
{
    new Thread(new ThreadStart(fooUsingSleep)).Start();

    callToMethodAfterInterval(new Action<object, ElapsedEventArgs>(fooUsingTimer), 3000);
}

public static void fooUsingSleep()
{
    Console.WriteLine("Doing some consuming time work using sleep");
    Thread.Sleep(3000);
    fooUsingSleep();
}

public static void fooUsingTimer(object dummy, ElapsedEventArgs dummyElapsed)
{
    Console.WriteLine("Doing some consuming time work usning timer");
    callToMethodAfterInterval(new Action<object, ElapsedEventArgs>(fooUsingTimer), 3000);
}

public static void callToMethodAfterInterval(Action<object,ElapsedEventArgs> inMethod, int inInterval)
{
    System.Timers.Timer myTimer = new System.Timers.Timer();
    myTimer.Elapsed += new ElapsedEventHandler(inMethod);
    myTimer.Interval = inInterval;
    myTimer.AutoReset = false;
    myTimer.Start();
}

So my questions are

1)Can I wrote the code with the timer more elegant? Means removing the call to the callToMethodAfterInterval method from fooUsingTimer, make the timer one or two lines, and remove the dummy variables from the declaration of fooUsingTimer?

2)I understand sleep isn't busy waiting (http://www.codeproject.com/KB/threads/ThreadingDotNet.aspx) So I don't found justification to use the timer option here, because the sleep is more simple, what is better to use, the timer version or the sleep one?

3)I know that Timers.timer is thread safe, does it can help me in the behavior I want to implement?

Thanks.

like image 865
Delashmate Avatar asked Jan 13 '11 11:01

Delashmate


People also ask

What does sleep timer on Spotify do?

Select how long you want your tunes to play. You can choose to have the music go for 5 minutes, 10 minutes, 15 minutes, 30 minutes, 45 minutes, 1 hour, or until the end of the track. An on-screen notification will say “OK, your sleep timer is set.”

What is a sleep timer used for?

Conclusion. If you want to have a longer battery life on your device, employing the sleep timer is a great way to do so. You set your computer to a specific duration of time, and after that amount of time has passed, your device will automatically shut off.

Did Spotify remove sleep timer?

Spotify does have a sleep timer function on the mobile app for iOS and Android. For music playback: start playing a song and then fullscreen it by tapping its title from the playback bar at the bottom of the app. Press ⋮ > Sleep timer.

Can I set a timer for my phone to turn off?

Tap [Power management]. Tap the button displayed to the right of [Power off timer]. [Disabled] is selected by default. Select the time you want the power of this unit to automatically turn off, and tap it.


2 Answers

Do you realize that fooUsingSleep is calling itself over and over? It will eventually generate a stack overflow.

If you are using timer, it can be as simple as this:

    System.Windows.Forms.Timer t = new System.Windows.Forms.Timer();
    t.Interval = 3000;
    t.Tick += new EventHandler((o,ea) => Console.WriteLine("foo"));
like image 105
Axarydax Avatar answered Oct 19 '22 21:10

Axarydax


The real context of your program matters too.

The sleep option 'wastes' a Thread, not a problem in a small console app but in general not a good idea.

You don't need to restart the timer, the following will keep ticking:

    static void Main(string[] args)
    {
        var t = new System.Timers.Timer(1000);
        t.Elapsed += (s, e) => CallMeBack();
        t.Start();

        Console.ReadLine();
    }
like image 36
Henk Holterman Avatar answered Oct 19 '22 21:10

Henk Holterman