locked
Repeated event using task.delay vs a timer RRS feed

  • Question

  • I'm just wondering if there's any practical difference (and possibly if one is better than the other) between creating a repeating event using a timer, or by simply calling task.delay in an infinite loop in an async function.

    For example, let's say you want to autosave every 10 seconds. You could either use a timer.

    However, you could also create a an AutoSaver class, which basically looks as follows.

    public class AutoSaver
            {
                private int _save_interval;
                private bool _stop_saving = false;
    
    
                public AutoSaver(double interval)
                {
                    _save_interval = (int)(interval * 1000);
                    _stop_saving = false;
                }
    
                public async void StartSaving()
                {
                    _stop_saving = false;
                    await Task.Delay(_save_interval);
                    while (!_stop_saving)
                    {
                        SaveDocument(...);
                        await Task.Delay(_save_interval);
                    }
                }
    
                public void StopSaving()
                {
                    _stop_saving = true;
                }
            }

    Is there any good reason to not use the AutoSaver?

    Cheers,

    Tomas

    Thursday, October 11, 2012 7:52 PM

Answers

All replies

  • Why you don´t save only when something is changed?

    Or have object that will see if there is changes and if the last time that something was changed is greater than X you save it?


    Sara Silva

    Thursday, October 11, 2012 9:07 PM
  • Well, I just used an example. It's really not about auto-saving at all.

    It's more about what to do about it when you have a periodic event like that, and if instead of creating a timer simply using a construct with an async function that contains an action and a task.delay inside a while loop.

    Thursday, October 11, 2012 10:29 PM
  • Hi,

    Based on my knowledge,the Task will be excuted on another thread rather than main thread if it is running asynchronously and the Task.Delay will block the thread for a specifiied time before returning to main thread. While DispatchTimer will create a thread which can provide a timer and call the callback in a specified interval of time, the callback is running on the main thread which provides UI.

    reference:

    Task.Delay:

    http://msdn.microsoft.com/en-us/library/hh194873.aspx

    DispatchTimer:

    http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.dispatchertimer.aspx

    Hope this helps


    Aaron Xue [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    • Edited by Aaron Xue Friday, October 12, 2012 9:00 AM
    • Marked as answer by Aaron Xue Tuesday, October 23, 2012 1:46 PM
    Friday, October 12, 2012 8:59 AM
  • Thanks for the reply.

    It seems to me that the two approaches do pretty much the same thing though. Wait for x amount of second in a background thread, then return to the UI thread and execute some action. Repeat.

    So, I guess what I really want to know is there some big difference between the two,  or some compelling reason to use one over the other?

    It seems to me that they do pretty much the same thing.

    Cheers,

    Tomas

    Friday, October 12, 2012 4:14 PM
  • Hi,

    The async operation in Task is running on another thread rather than main thread. The timer call a callback method in a specified interval of time and the callback method is running in the main thread.

    The difference between them is process method following the delay, The process following "await somethingAsync()" will be executed immediately because it has to make the main thread which provides UI be respondable to user. While the callback method called by the timer will be executed in a specified interval of time. 

    Hope this helps


    Aaron Xue [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    • Edited by Aaron Xue Monday, October 15, 2012 9:59 AM
    Monday, October 15, 2012 9:58 AM