/// <summary>
 /// Ends any ongoing processing and closes the processor
 /// </summary>
 public void Close()
 {
     if (LiveProcessingQueue != null)
     {
         LiveProcessingQueue.CompleteAdding();
     }
 }
Пример #2
0
        private void StartWorkers(List <GlobalConfig.SignatureSource> repos, int workerCount, CancellationToken cancelToken)
        {
            _repos = new System.Collections.Concurrent.BlockingCollection <GlobalConfig.SignatureSource>();
            repos.ForEach(x => _repos.Add(x));
            _repos.CompleteAdding();
            Running            = true;
            _token             = cancelToken;
            InitialTaskCount   = repos.Count;
            CompletedTaskCount = 0;

            _activeWorkers = workerCount;
            for (var i = 0; i < workerCount; i++)
            {
                Task.Factory.StartNew(StartWork);
            }
        }
Пример #3
0
        /// <summary>
        /// Performs dispose logic, can be overridden by derivded types.
        /// </summary>
        /// <param name="disposing">True if the pool is being explicitly disposed, false if it is being disposed from a finalizer.</param>
        /// <seealso cref="PoolBase{T}.Dispose()"/>
        /// <seealso cref="PoolBase{T}.IsDisposed"/>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                _ItemsToInitialise?.CompleteAdding();

                if (IsPooledTypeDisposable)
                {
                    while (!_Pool.IsEmpty)
                    {
                        _Pool.TryTake(out var item);
                        SafeDispose(item);
                    }
                }
            }
        }
Пример #4
0
        private bool disposedValue = false; // To detect redundant calls

        /// <summary>
        /// Wait and dispose pending log resources.
        /// </summary>
        /// <param name="disposing">Disposing resource.</param>
        protected virtual void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                if (disposing)
                {
                    // TODO: dispose managed state (managed objects).
                    _PendingLogs.CompleteAdding();
                    _WriterTask.Wait();
                }

                // TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
                // TODO: set large fields to null.

                disposedValue = true;
            }
        }
Пример #5
0
        static void ProcessFile(string input, string output)
        {
            var inputsLine     = new System.Collections.Concurrent.BlockingCollection <string>();
            var processedLines = new System.Collections.Concurrent.BlockingCollection <string>();

            // stage #1
            var readLines = Task.Factory.StartNew(() =>
            {
                try
                {
                    foreach (var line in File.ReadLines(input))
                    {
                        inputsLine.Add(line);
                    }
                }
                finally { inputsLine.CompleteAdding(); }
            });

            // stage #2
            var processLines = Task.Factory.StartNew(() =>
            {
                try
                {
                    foreach (var line in inputsLine.GetConsumingEnumerable()
                             .Select(line => Regex.Replace(line, @"\s+", ", ")))
                    {
                        processedLines.Add(line);
                    }
                }
                finally { processedLines.CompleteAdding(); }
            });

            // stage #3
            var writeLines = Task.Factory.StartNew(() =>
            {
                File.WriteAllLines(output, processedLines.GetConsumingEnumerable());
            });

            Task.WaitAll(readLines, processLines, writeLines);
        }
Пример #6
0
        static void Main()
        {
            // create the state machine model
            var model = new StateMachine <Player>("model");

            // create the vertices within the model
            var initial     = model.CreatePseudoState("initial", PseudoStateKind.Initial);
            var operational = model.CreateState("operational");
            var choice      = model.CreatePseudoState("choice", PseudoStateKind.Choice);
            var flipped     = model.CreateState("flipped");
            var final       = model.CreateFinalState("final");

            var history = operational.CreatePseudoState("history", PseudoStateKind.DeepHistory);
            var stopped = operational.CreateState("stopped");
            var active  = operational.CreateState("active").Entry(i => i.EngageHead()).Exit(i => i.DisengageHead());

            var running = active.CreateState("running").Entry(i => i.StartMotor()).Exit(i => i.StopMotor());
            var paused  = active.CreateState("paused");

            // create the transitions between vertices of the model
            initial.To(operational).Effect(i => i.DisengageHead()).Effect(i => i.StopMotor());
            history.To(stopped);
            stopped.To(running).When <string>(command => command == "play");
            active.To(stopped).When <string>(command => command == "stop");
            running.To(paused).When <string>(command => command == "pause");
            running.To().When <string>(command => command == "tick").Effect((Player instance) => instance.Count++);
            paused.To(running).When <string>(command => command == "play");
            operational.To(final).When <string>(command => command == "off");
            operational.To(choice).When <string>(command => command == "rand");
            choice.To(operational).Effect(() => Console.WriteLine("- transition A back to operational"));
            choice.To(operational).Effect(() => Console.WriteLine("- transition B back to operational"));
            operational.To(flipped).When <string>(command => command == "flip");
            flipped.To(operational).When <string>(command => command == "flip");

            // validate the model for correctness
            model.Validate();

            // create a blocking collection make events from multiple sources thread-safe
            var queue = new System.Collections.Concurrent.BlockingCollection <Object>();

            // create an instance of the player - enqueue a tick message for the machine while its playing
            var player = new Player(() => queue.Add("tick"));

            // initialises the players initial state (enters the region for the first time, causing transition from the initial PseudoState)
            model.Initialise(player);

            // create a task to capture commands from the console in another thread
            System.Threading.Tasks.Task.Run(() => {
                string command = "";

                while (command.Trim().ToLower() != "exit")
                {
                    queue.Add(command = Console.ReadLine());
                }

                queue.CompleteAdding();
            });

            // write the initial command prompt
            Console.Write("{0:0000}> ", player.Count);

            // process messages from the queue
            foreach (var message in queue.GetConsumingEnumerable())
            {
                // process the message
                model.Evaluate(player, message);

                // manage the command prompt
                var left = Math.Max(Console.CursorLeft, 6);
                var top  = Console.CursorTop;
                Console.SetCursorPosition(0, top);
                Console.Write("{0:0000}>", player.Count);
                Console.SetCursorPosition(left, top);
            }
        }
Пример #7
0
		static void Main () {
			// create the state machine model
			var model = new StateMachine<Player>("model");

			// create the vertices within the model
			var initial = model.CreatePseudoState("initial", PseudoStateKind.Initial);
			var operational = model.CreateState("operational");
			var choice = model.CreatePseudoState("choice", PseudoStateKind.Choice);
			var flipped = model.CreateState("flipped");
			var final = model.CreateFinalState("final");

			var history = operational.CreatePseudoState("history", PseudoStateKind.DeepHistory);
			var stopped = operational.CreateState("stopped");
			var active = operational.CreateState("active").Entry(i => i.EngageHead()).Exit(i => i.DisengageHead());

			var running = active.CreateState("running").Entry(i => i.StartMotor()).Exit(i => i.StopMotor());
			var paused = active.CreateState("paused");

			// create the transitions between vertices of the model
			initial.To(operational).Effect(i => i.DisengageHead()).Effect(i => i.StopMotor());
			history.To(stopped);
			stopped.To(running).When<string>(command => command == "play");
			active.To(stopped).When<string>(command => command == "stop");
			running.To(paused).When<string>(command => command == "pause");
			running.To().When<string>(command => command == "tick").Effect((Player instance) => instance.Count++);
			paused.To(running).When<string>(command => command == "play");
			operational.To(final).When<string>(command => command == "off");
			operational.To(choice).When<string>(command => command == "rand");
			choice.To(operational).Effect(() => Console.WriteLine("- transition A back to operational"));
			choice.To(operational).Effect(() => Console.WriteLine("- transition B back to operational"));
			operational.To(flipped).When<string>(command => command == "flip");
			flipped.To(operational).When<string>(command => command == "flip");

			// validate the model for correctness
			model.Validate();

			// create a blocking collection make events from multiple sources thread-safe
			var queue = new System.Collections.Concurrent.BlockingCollection<Object>();

			// create an instance of the player - enqueue a tick message for the machine while its playing
			var player = new Player(() => queue.Add("tick"));

			// initialises the players initial state (enters the region for the first time, causing transition from the initial PseudoState)
			model.Initialise(player);

			// create a task to capture commands from the console in another thread
			System.Threading.Tasks.Task.Run(() => {
				string command = "";

				while (command.Trim().ToLower() != "exit") {
					queue.Add(command = Console.ReadLine());
				}

				queue.CompleteAdding();
			});

			// write the initial command prompt
			Console.Write("{0:0000}> ", player.Count);

			// process messages from the queue
			foreach (var message in queue.GetConsumingEnumerable()) {
				// process the message
				model.Evaluate(player, message);

				// manage the command prompt
				var left = Math.Max(Console.CursorLeft, 6);
				var top = Console.CursorTop;
				Console.SetCursorPosition(0, top);
				Console.Write("{0:0000}>", player.Count);
				Console.SetCursorPosition(left, top);
			}
		}