Invokes an action after a delay times out (cancelling any previous actions that may be pending).
This can be used to invoke something after a series of rapid events have fired, like key-strokes size changed.
Inheritance: DisposableBase
        public PropertyExplorerPanelViewModel()
        {
            // Setup initial conditions.
            testHarnessModel = TestHarnessModel.Instance;
            delayedAction = new DelayedAction(0.1, OnDelayTimedOut);
            propertyExplorerModel = new PropertyExplorerViewModel { GetCategory = GetCategory, GetProperties = GetProperties };

            // Load settings.
            storedSettings = testHarnessModel.Settings.PropertyExplorer;
            SelectedDataOption = storedSettings.ReadPropertyDataFrom;
            propertyExplorerModel.IncludeHierarchy = storedSettings.IncludeHierarchy;

            // Wire up events.
            testHarnessModel.PropertyChanged += (sender, e) =>
                                       {
                                           if (e.PropertyName == TestHarnessModel.PropCurrentClass) OnCurrentClassChanged();
                                       };

            propertyExplorerModel.PropertyChanged += (sender, e) =>
                                     {
                                         if (e.PropertyName == PropertyExplorerViewModel.PropSelectedObject) OnPropertyChanged(PropSelectedObject);
                                         if (e.PropertyName == PropertyExplorerViewModel.PropIncludeHierarchy)
                                         {
                                             storedSettings.IncludeHierarchy = propertyExplorerModel.IncludeHierarchy;
                                             SaveSettings();
                                         }
                                     };
        }
        public OutputLogViewModel(IOutput writer, OutputLog control)
        {
            // Store values.
            Writer = writer;
            Control = control;

            // Create objects.
            Strings = new StringLibrary();
            Lines = new ObservableCollection<OutputLineViewModel>();
            scrollDelay = new DelayedAction(0.1, ScrollToBottom);

            // Create brushes.
            dividerColor = new SolidColorBrush(Colors.Black) { Opacity = 0.1 };
            lineBreakColor = new SolidColorBrush(Color.FromArgb(255, 255, 0, 228)) { Opacity = 0.4 };

            // Create commands.
            ClearCommand = new DelegateCommand<Button>(m => Clear(), m => IsClearButtonEnabled);

            // Wire up events.
            writer.WrittenTo += HandleWrittenTo;
            writer.Cleared += delegate { Clear(); };
            writer.BreakInserted += delegate { InsertBreak(); };

            // Finish up.
            UpdateLineMargin();
        }
        public DisplayOptionToolbarViewModel()
        {
            // Retrieve the settings model.
            var settings = TestHarnessModel.Instance.Settings;
            Model = settings.ControlDisplayOptionSettings;

            // Setup the save delay.
            saveDelay = new DelayedAction(0.2, () => settings.Save());

            // Wire up events.
            Model.PropertyChanged += delegate { saveDelay.Start(); };
        }
        public HtmlBlock()
        {
            // Setup initial conditions.
            InitializeComponent();
            HtmlElementId = string.Format("{0}_{1}", GetType().Name, Guid.NewGuid().ToString());

            // Create update delay.
            // NB: This is put on a delay (of zero, which causes it to be invoked asynchronously)
            // because Google Chrome crashes otherwise.  Too many calls, to quickly to the JavaScript engine.
            updateDelay = new DelayedAction(0, UpdateDimensions);

            // Wire up events.
            Loaded += OnLoaded;
        }
        public void ShouldNotCallActionWhenStopped()
        {
            DelayedAction action2 = null;

            var wasCalled = false;
            Action callback2 = delegate
                                  {
                                      action2.IsRunning.ShouldBe(false);
                                      wasCalled.ShouldBe(false);
                                      EnqueueTestComplete();
                                  };

            var action1 = new DelayedAction(0.2, ()=> wasCalled = true);
            action2 = new DelayedAction(1, callback2);

            action1.Start();
            action2.Start();

            action1.Stop();
        }
 /// <summary>Invokes the given action after the specified delay.</summary>
 /// <param name="delay">The delay (in seconds) before invoking the action.</param>
 /// <param name="action">The action to invoke.</param>
 /// <remarks>Returns the 'DelayedAction' used to invoke the method (can be used to cancel the delayed invoke operation).</remarks>
 public static DelayedAction Invoke(double delay, Action action)
 {
     var delayedAction = new DelayedAction(delay, action);
     delayedAction.Start();
     return delayedAction;
 }
 public void ShouldUpdateIsRunningProperty()
 {
     DelayedAction action = null;
     Action callback = delegate
                                     {
                                         action.IsRunning.ShouldBe(false);
                                         EnqueueTestComplete();
                                     };
     action = new DelayedAction(0.1, callback);
     action.Start();
     action.IsRunning.ShouldBe(true);
 }
 public void ShouldInvokeAfterTimeout()
 {
     Action callback = EnqueueTestComplete;
     var action = new DelayedAction(0.2, callback);
     action.Start();
 }
 public void ShouldStoreSeconds()
 {
     var action = new DelayedAction(5, null);
     action.Delay.ShouldBe(5.0);
 }
        public void ShouldFireInvokedEvent()
        {
            var fireCount = 0;
            var fireCountDuringAction = 0;

            var delayedAction = new DelayedAction(0.01, () =>
                                                            {
                                                                fireCountDuringAction = fireCount;
                                                            });

            delayedAction.ActionInvoked += delegate { fireCount++; };
            delayedAction.Start();

            DelayedAction.Invoke(0.2, () =>
                                          {
                                              fireCountDuringAction.ShouldBe(0);
                                              fireCount.ShouldBe(1);
                                              EnqueueTestComplete();
                                          });
        }
        public void ShouldFireStoppedEvent()
        {
            var delayedAction = new DelayedAction(0.1, () => { });

            var fireCount = 0;
            delayedAction.Stopped += delegate { fireCount++; };

            delayedAction.Stop();
            fireCount.ShouldBe(0);

            delayedAction.Start();
            delayedAction.Stop();
            fireCount.ShouldBe(1);

            delayedAction.Stop();
            fireCount.ShouldBe(1);
        }
        public void ShouldStopOnDispose()
        {
            var action = new DelayedAction(1, () => { });

            action.Start();
            action.IsRunning.ShouldBe(true);

            action.Dispose();
            action.IsRunning.ShouldBe(false);
        }
        public void ShouldRunSynchronously()
        {
            var value = 0;
            DelayedAction.IsAsyncronous = false;
            var action = new DelayedAction(0.1, () => value ++);

            action.Start();
            value.ShouldBe(1);
        }