Example #1
0
        /// <summary>
        /// Initializes this instance.
        /// </summary>
        /// <returns>Returns an awaitable Task</returns>
        public Task Initialize()
        {
            if (this.isDisposed)
            {
                throw new ObjectDisposedException(typeof(Autosave <TComponent>).Name, "Can not initialize the autosave as it has already been disposed of.");
            }

            // Default to saving every 60 seconds if the frequency is under 1 second.
            if (this.AutoSaveFrequency < 1)
            {
                this.AutoSaveFrequency = 60;
            }

            this.autosaveTimer = new EngineTimer <TComponent>(this.ItemToSave);
            double autosaveInterval = TimeSpan.FromSeconds(this.AutoSaveFrequency).TotalMilliseconds;

            this.autosaveTimer.StartAsync(
                autosaveInterval,
                autosaveInterval,
                0,
                async(game, timer) =>
            {
                await this.saveDelegate();
                MessageBrokerFactory.Instance.Publish(new AutosaveMessage <TComponent>(this.ItemToSave));
            });

            return(Task.FromResult(true));
        }
Example #2
0
 /// <summary>
 /// Starts the state clock at the specified interval, firing the callback provided.
 /// </summary>
 /// <param name="interval">The interval.</param>
 /// <param name="callback">The callback.</param>
 void StartStateClock(double interval, Action <ITimeOfDay> callback)
 {
     // If the minute interval is less than 1 second,
     // then we increment by the hour to reduce excess update calls.
     this.timeOfDayClock = new EngineTimer <ITimeOfDay>(this.CurrentTime);
     this.timeOfDayClock.Start(interval, interval, 0, (state, clock) =>
     {
         callback(state);
         this.OnTimeUpdated();
     });
 }