private void OnTriggerExecute(TriggerBase trigger) { lock (_localSync) { _tokenSource = new CancellationTokenSource(); var token = _tokenSource.Token; _triggerTask = Task.Run(() => { if (Core.Now - _lastUpdateTime > WaitTime) { _lastUpdateTime = Core.Now; BeforeInvoke?.Invoke(this, new TriggeredEventArgs <TriggeredActionBase>(trigger, this)); var loadOk = true; var sw = Stopwatch.StartNew(); try { if (CancellationToken.IsCancellationRequested) { return; } OnAction(); if (CancellationToken.IsCancellationRequested) { return; } sw.Stop(); } catch (Exception ex) { sw.Stop(); loadOk = false; OnException?.Invoke(this, new ExceptionEventArgs(ex, sw.Elapsed.TotalMilliseconds)); } if (loadOk) { AfterInvoke?.Invoke(this, new TriggeredEventArgs <TriggeredActionBase>(trigger, this, sw.Elapsed.TotalMilliseconds)); } } else { NotInvokedTooRecent?.Invoke(this, new TriggeredEventArgs <TriggeredActionBase>(trigger, this)); } }, token); try { _triggerTask.Wait(token); } catch { // ignored } _triggerTask = null; _tokenSource = null; } }
/// <summary> /// Remove an update trigger from the collection /// </summary> /// <param name="trigger">Update trigger object</param> public void RemoveTrigger(TriggerBase trigger) { if (trigger is null) { return; } if (!_triggers.Contains(trigger)) { return; } trigger.OnTriggered -= OnTriggerExecute; trigger.Dispose(); _triggers.Remove(trigger); }
/// <summary> /// Add an update trigger to the collection /// </summary> /// <param name="trigger">Update trigger object</param> public void AddTrigger(TriggerBase trigger) { if (trigger is null) { return; } if (_triggers.Contains(trigger)) { return; } trigger.OnTriggered += OnTriggerExecute; trigger.Init(); _triggers.Add(trigger); }
/// <inheritdoc /> /// <summary> /// Triggered instance event args /// </summary> /// <param name="trigger">Trigger instance</param> /// <param name="value">Current Value</param> /// <param name="elapsed">Time elapsed</param> public TriggeredEventArgs(TriggerBase trigger, T value, double elapsed = 0) { Trigger = trigger; Value = value; Elapsed = elapsed; }
private void ChildTrigger(TriggerBase child) { Core.Log.LibVerbose("{0}: Plan Trigger call", GetType().Name); Trigger(); }