Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Timers;
- namespace ProcessShredder.Updating
- {
- public interface IUpdate
- {
- /// <summary>
- /// A method used to update parts of your object.
- /// When used with GUIs or thread sensative objects
- /// you should invoke their thread to run code
- /// </summary>
- void TimedUpdate();
- }
- public class Updater
- {
- /// <summary>The object being updated</summary>
- public IUpdate Obj { get; set; }
- /// <summary>The time between update events</summary>
- public int Interval { get; set; }
- private Timer timer = null;
- /// <summary>Sends timed update messages to IUpdate classes</summary>
- /// <param name="obj">The object to update</param>
- public Updater(IUpdate obj)
- {
- Obj = obj;
- }
- /// <summary>Begins sending update messages</summary>
- /// <param name="interval">Frequency in milliseconds</param>
- /// <returns>true if the operation was a success, false otherwise</returns>
- public bool StartTimedUpdates(int interval)
- {
- if (interval > 0)
- {
- Interval = interval;
- timer = new Timer(interval);
- timer.Elapsed += OnTimedEvent;
- timer.AutoReset = false;
- timer.Enabled = true;
- return true;
- }
- return false;
- }
- /// <summary>
- /// Called every 'Interval' milliseconds after StarTimedUpdates
- /// succeeds; calls TimedUpdate() for the stored object
- /// </summary>
- private void OnTimedEvent(Object source, ElapsedEventArgs e)
- {
- if (Obj != null)
- {
- Obj.TimedUpdate();
- }
- if (timer != null)
- {
- timer.Interval = Interval;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment