Ejemplo n.º 1
0
        public async Task StartProducingAsync(int numberOfFiles, CancellationToken cancellationToken)
        {
            var i = 0;

            while (!cancellationToken.IsCancellationRequested && i < numberOfFiles)
            {
                var fileName = $"{Guid.NewGuid()}.txt";
                var number   = Random.Next(1, 50);

                var printJob = new PrintJob(fileName, number);

                Console.WriteLine($"#{Name}# produzindo arquivo '{printJob.Name}', número de páginas {printJob.NumberOfPages}.");

                try
                {
                    _queue.AddBack(printJob);
                }
                catch (FullQueueException)
                {
                    // TODO: Tentar novamente, gravar logs, etc...
                    Console.WriteLine($"#{Name}# FullQueueException");
                }

                var delay = Random.Next(1, 5);

                await Task.Delay(TimeSpan.FromSeconds(delay));

                i++;
            }

            Console.WriteLine($"#{Name}# Finalizado");
        }
Ejemplo n.º 2
0
        public Producer(string nome, CircularQueue queue)
        {
            _queue = queue;

            Random numbeRandom = new Random();

            Thread thread = new Thread(() =>
            {
                if (QueuePrint.Count == 0)
                {
                    return;
                }

                var queuePrint = QueuePrint.Dequeue();

                if (queuePrint != null)
                {
                    Console.WriteLine($"#{nome}#: produzindo arquivo '{queuePrint.NameQueue}', número de páginas {queuePrint.PageQueue}.");
                    var print = new PrintJob(queuePrint.NameQueue, queuePrint.PageQueue);
                    _queue.AddBack(print);
                }

                int numberOfSeconds = numbeRandom.Next(1, 5);
                var count           = 0;

                while (count < numberOfSeconds)
                {
                    Thread.Sleep(1000);
                    count++;
                }
            });

            thread.Start();
        }