public bool Broadcast(string key, params object[] message)
        {
            List <WeakAction> wr;

            lock (_registeredHandlers)
            {
                if (!_registeredHandlers.TryGetValue(key, out wr))
                {
                    return(false);
                }
            }

            foreach (var cb in wr)
            {
                Delegate action = cb.GetMethod();
                if (action == null)
                {
                    continue;
                }
                switch (cb.TaskType)
                {
                case TaskType.Background:
                    // check if already running
                    if (!_baseQueue.ContainsKey(cb.Target.Target))
                    {
                        var bgTask = _backgroundTaskFactory.StartNew(() => action.DynamicInvoke(message));
                        _baseQueue.Add(cb.Target.Target, bgTask);
                    }
                    break;

                case TaskType.Periodic:
                    var periodicTask = new PeriodicTask(() => action.DynamicInvoke(message), _budget);
                    periodicTask.Start(_preemptiveScheduler);
                    break;

                case TaskType.Sporadic:
                    // one Periodic to run all sporadics
                    var sporadicTask = new SporadicTask(() => action.DynamicInvoke(message), _budget);
                    sporadicTask.Start(_preemptiveScheduler);
                    break;

                case TaskType.LongRunning:
                    //UI
                    _staTaskFactory.StartNew(() => action.DynamicInvoke(message));
                    break;
                }
            }

            lock (_registeredHandlers)
            {
                wr.RemoveAll(wa => wa.HasBeenCollected);
            }

            return(true);
        }
Exemple #2
0
        public void Broadcast(DarkLightEvent darkLightEvent)
        {
            //Test w/ everything periodic for now
            //var taskType = TaskType.Periodic;
            object target = new object();

            object[] message = new object[1];
            System.Action <object[]> action = null;

            switch (darkLightEvent.EventType)//taskType)
            //TODO: refactor mapping below
            {
            //NOT BEING USED YET
            case (EventType.BacktestRequest):     //TaskType.Background:
                // check if already running
                if (!_baseQueue.ContainsKey(target))
                {
                    var bgTask = _backgroundTaskFactory.StartNew(() => _mediatorAdapter.Publish(darkLightEvent));
                    //_baseQueue.Add(target, bgTask);
                }
                break;

            case (EventType.Trade):
            case EventType.Status:    //TaskType.Periodic:
                var periodicTask = new PeriodicTask(() => _eventAggregator.Publish(darkLightEvent), _budget);
                periodicTask.Start(_preemptiveScheduler);
                break;

            /*
             * case EventType.BacktestRequest://TaskType.Sporadic:
             *  // one Periodic to run all sporadics
             *  var sporadicTask1 = new SporadicTask(() => _mediatorAdapter.Publish(darkLightEvent), _budget);
             *  sporadicTask1.Start(_preemptiveScheduler);
             *  break;
             */
            case EventType.LinkedNavigation:
                // one Periodic to run all sporadics
                var sporadicTask2 = new SporadicTask(() => _eventAggregator.Publish(darkLightEvent), _budget);
                sporadicTask2.Start(_preemptiveScheduler);
                break;

            //NOT BEING USED YET
            case EventType.Result:    //TaskType.LongRunning:
                //UI
                _staTaskFactory.StartNew(() => action.DynamicInvoke(message));
                break;
            }
        }