Пример #1
0
        private void Worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            _runContext   = null;
            _token.Worker = null;
            _token        = null;

            if (e.Error != null)
            {
                if (_runContext.OnError != null)
                {
                    _runContext.OnError();
                }
            }
            else if (e.Cancelled)
            {
                if (_runContext.OnCancelled != null)
                {
                    _runContext.OnCancelled();
                }
            }
            else
            {
                if (_runContext.OnSuccess != null)
                {
                    _runContext.OnSuccess(e.Result);
                }
            }
        }
Пример #2
0
        public IWorkerToken Start(RunContext runContext)
        {
            if (runContext == null)
            {
                throw new ArgumentNullException();
            }

            if (_runContext != null)
            {
                throw new InvalidOperationException();
            }

            _runContext = runContext;
            _worker.RunWorkerAsync();
            _token = new WorkerToken(_worker);
            return(_token);
        }
Пример #3
0
        public IDisposable RunEventSource(IEventSource source)
        {
            Assert.ArgumentNotNull(source, nameof(source));

            IEventSourceContext context = new EventSourceContext(_messageBus);
            var strategy = new EventSourceWorkStrategy(source, context);
            var thread   = new IntervalWorkerThread(_messageBus.Logger, strategy);

            if (!_threads.TryAdd(thread.Id, thread))
            {
                _logger.Error($"Could not add new event source ThreadId={thread.Id}. Maybe it has already been added?");
                thread.Dispose();
                return(null);
            }

            var threadToken = _messageBus.WorkerPool.RegisterManagedThread("Event Source Module", thread.ThreadId, "SourceModule thread " + thread.Id);
            var workerToken = new WorkerToken(this, thread, thread.Id, threadToken);

            _tokens.TryAdd(thread.Id, workerToken);
            thread.Start();
            return(workerToken);
        }