public static void ProduceTasks() { Random random = new Random(); for (int i = 1; i <= 1; i++) { var queue = new QueuedObject { QueueID = i, ProducerThreadID = Thread.CurrentThread.ManagedThreadId, EnqueueDateTime = DateTime.Now, // Used to Generate Random String RandomString = new string(Enumerable.Repeat("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", 5).Select(s => s[random.Next(s.Length)]).ToArray()) }; #region Queueing using Queue Collection // Uncomment QueueService.Enqueue(() => { ProcessQueue(queue); }) to use .Net Queue Object to Queue Tasks. // And Comment ConcurrentQueueService.Enqueue QueueService.Enqueue(() => { ProcessQueue(queue); }); #endregion #region Queueing using ConcurrentQueue //ConcurrentQueueService.Enqueue(() => { ProcessQueue(queue); }); #endregion Console.WriteLine ( "Enqueued: " + queue.QueueID + "\t" + "Producer ThreadID :" + queue.ProducerThreadID + "\t" + queue.EnqueueDateTime.ToLongTimeString() + "\t" + "RandomString :" + queue.RandomString ); } }
public void ProduceItems()//Function to enqueue the items in to the queue { string[] files = Directory.GetFiles(@"C:\\ProgrammerTest\Data\Computers"); int i = 0; foreach (var file in files) { var queue = new QueuedObject { QueueID = i, ProducerThreadID = Thread.CurrentThread.ManagedThreadId, EnqueueDateTime = DateTime.Now, inputXml = GetXml(file), xsltString = GetXml(@"C:\\ProgrammerTest\Resources\Computer.xslt"), filename = Path.GetFileName(file) }; _qs.Enqueue(queue); Console.WriteLine ( "Enqueued: " + queue.QueueID + "\t" + "Producer ThreadID :" + queue.ProducerThreadID + "\t" + queue.EnqueueDateTime.ToLongTimeString() + "\t" + "XmlString :" + queue.inputXml ); i++; } }
private int ParseNumber(string s, int start, bool positive) { // Ganzzahl string zahl = ""; int i; for (i = start; i < s.Length; i++) { if (!Char.IsDigit(s[i]) || i == s.Length - 1) { zahl = s.Substring(start, i - start + (i == s.Length - 1 ? 1:0)); break; } } ValueType number = null; if (number == null) { try { number = sbyte.Parse(zahl); } catch { } } if (number == null) { try { number = short.Parse(zahl); } catch { } } if (number == null) { try { number = int.Parse(zahl); } catch { } } if (number == null) { try { number = Int64.Parse(zahl); } catch { } } if (number == null) { try { number = long.Parse(zahl); } catch { } } if (number == null) { try { number = ulong.Parse(zahl); } catch { } } if (stack.Count > 0 && stack.PeekType <QueuedObject>()) { QueuedObject obj = stack.Pop <QueuedObject>(); stack.Push(number); if (--obj.expLeft == 0) { obj.op.calc(this, stack); } } else { stack.Push(number); } return(i); }
public void ConsumeItems() // Consume Items from the queue { while (true) { QueuedObject qo = _qs.Dequeue(); if (qo != null) { ProcessQueue(qo); } } }
public static void ProcessQueue(QueuedObject queue) { string reversedString = new string(Enumerable.Range(1, queue.RandomString.Length).Select(i => queue.RandomString[queue.RandomString.Length - i]).ToArray()); Console.WriteLine ( "Dequeued: " + queue.QueueID + "\t" + "Consumer ThreadID :" + Thread.CurrentThread.ManagedThreadId + "\t" + DateTime.Now.ToLongTimeString() + "\t" + "ReversedString :" + reversedString ); }
public async Task <bool> Inserir(QueuedObject request) { if (request != null && Selecionar(request.MensagemId).Result == null) { _db.Queued.Add(request); await _db.SaveChangesAsync(); return(true); } UpdateMensagemIntegrada(request.MensagemId); return(false); }
public void ProcessQueue(QueuedObject queue) // Process the queue item { string processedHtmlstring = TransformXMLToHTML(queue.inputXml, queue.xsltString); Console.WriteLine ( "Dequeued: " + queue.QueueID + "\t" + "Consumer ThreadID :" + Thread.CurrentThread.ManagedThreadId + "\t" + DateTime.Now.ToLongTimeString() + "\t" + "processedHtmlstring :" + processedHtmlstring ); System.IO.File.WriteAllText(@"C:\\ProgrammerTest\Data\Output\" + queue.filename.ToLower().Replace(".xml", ".html"), processedHtmlstring); }
public void BuscarNaFila() { var host = _configuration.GetSection("QueueHost").Value; try { var factory = new ConnectionFactory() { HostName = host, RequestedHeartbeat = TimeSpan.FromMinutes(3), Port = AmqpTcpEndpoint.UseDefaultPort }; using (var connection = factory.CreateConnection()) { using (var channel = connection.CreateModel()) { while (channel.IsOpen) { Thread.Sleep(1); channel.QueueDeclare(queue: "Mensagem", durable: false, exclusive: false, autoDelete: false, arguments: null ); var consumer = new EventingBasicConsumer(channel); QueuedObject obj = new QueuedObject(); string message = ""; consumer.Received += (model, ea) => { var body = ea.Body.ToArray(); message = Encoding.UTF8.GetString(body); obj = JsonConvert.DeserializeObject <QueuedObject>(message); _queuedRepository.Inserir(obj); }; channel.BasicConsume(queue: "Mensagem", autoAck: true, consumer: consumer ); } } connection.Close(); } } catch { } }
public void EnqueueTask_AlwaysAddOneTask_ToQueue() { // Arrange var sut = this.CreateSystemUnderTest(); var queue = new QueuedObject { QueueID = 1, AdderThreadID = Thread.CurrentThread.ManagedThreadId, EnqueueDateTime = DateTime.Now, // Used to Generate Random String RandomString = new string(Enumerable.Repeat("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", 5).Select(s => s[random.Next(s.Length)]).ToArray()) }; var h = new Action(DoSomeAction); // Act sut.EnqueueTask(h); // Assert sut._workTaskQueue.Count.ShouldBe(1); }