Brod.Common.Tasks.TaskEngine.Start C# (CSharp) Метод

Start() публичный Метод

Start engine host. This will start each task in registered in this host.
public Start ( CancellationToken token, Int32 timeout ) : Task
token System.Threading.CancellationToken /// A CancellationToken to observe while waiting for the tasks to complete. ///
timeout System.Int32 /// The number of milliseconds to wait, or Infinite (-1) to wait indefinitely ///
Результат Task
        public Task Start(CancellationToken token, Int32 timeout)
        {
            return Task.Factory.StartNew(() =>
            {
                var watch = Stopwatch.StartNew();

                // Try to start all tasks
                var tasks = _tasks
                    .Select(p => Task.Factory.StartNew(() => p.Run(token)))
                    .ToArray();

                // Engine started
                SystemInformer.Notify(new EngineStarted(_taskNames));

                try
                {
                    // Wait for all tasks to be either completed or canceled
                    Task.WaitAll(tasks, timeout, token);
                }
                catch (OperationCanceledException)
                {
                    // Do nothing
                }

                // Engine stopped
                SystemInformer.Notify(new EngineStopped(watch.Elapsed));
            });
        }

Usage Example

Пример #1
0
        public void Start()
        {
            using(var store = new Store(_configuration))
            {
                var handlers = new RequestHandlers(_configuration, store);

                var engine = new TaskEngine(
                    new SocketListener(ZMQ.SocketType.REP, _configuration.Port, handlers.MapHandlers),
                    new SocketListener(ZMQ.SocketType.PULL, _configuration.PullPort, handlers.MapHandlers),
                    new Flusher(_configuration, store));

                using (var token = new CancellationTokenSource())
                using (engine)
                {
                    var task1 = engine.Start(token.Token, Timeout.Infinite);

                    if (task1.Wait(Timeout.Infinite))
                        Console.WriteLine("Done without forced cancelation"); // This line shouldn't be reached
                    else
                        Console.WriteLine("\r\nRequesting to cancel...");

                    token.Cancel();
                }
            }
        }