private void DoWork(object message) { QueueMessage item; lock (MessageWorkItem) { item = MessageWorkItem.Where(x => x.Status.Equals(QueueMessageStatus.NotStarted)).FirstOrDefault(); if (item == null) { ((ManualResetEvent)manualResetEvent).Set(); doneEvents.Remove((ManualResetEvent)manualResetEvent); return; } Thread.CurrentThread.Priority = item.Module.Equals("SLM") ? ThreadPriority.Lowest : ThreadPriority.AboveNormal; item.Status = QueueMessageStatus.Started; Console.ForegroundColor = ConsoleColor.Magenta; Console.WriteLine($"[{DateTime.Now.ToString()}] Start Processing. {item.ToString()}", ConsoleColor.Magenta); } Worker.Calculate(new Random().Next(0, 50)); lock (MessageWorkItem) { item.Status = QueueMessageStatus.Finished; MessageWorkItem.Remove(item); } Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine($"[{DateTime.Now.ToString()}] End Processing. {item.ToString()}", ConsoleColor.Green); ((ManualResetEvent)manualResetEvent).Set(); doneEvents.Remove((ManualResetEvent)manualResetEvent); }
private void StartWorkStream() { try { PipeSecurity ps = new PipeSecurity(); ps.AddAccessRule(new PipeAccessRule("Users", PipeAccessRights.FullControl, AccessControlType.Allow)); ps.AddAccessRule(new PipeAccessRule("CREATOR OWNER", PipeAccessRights.FullControl, AccessControlType.Allow)); ps.AddAccessRule(new PipeAccessRule("SYSTEM", PipeAccessRights.FullControl, AccessControlType.Allow)); ps.AddAccessRule(new PipeAccessRule("Everyone", PipeAccessRights.FullControl, AccessControlType.Allow)); NamedPipeServerStream pipeStream = new NamedPipeServerStream("PipeTo" + "[Work]Telemetry", PipeDirection.InOut, 3, PipeTransmissionMode.Message, PipeOptions.WriteThrough, 1024, 1024, ps); pipeStream.WaitForConnection(); StreamReader streamReader = new StreamReader(pipeStream); string module = string.Empty, message = string.Empty, messageType = string.Empty, messageText = string.Empty; while ((message = streamReader.ReadLine()) != null) { module = message.Split(';')[0]; messageType = message.Split(';')[1]; messageText = message.Split(';').Length == 3 ? message.Split(';')[2] : string.Empty; switch (messageType) { case "Connected": // Console.WriteLine($"Connected: {module}", ConsoleColor.DarkCyan); break; case "Message": // Console.WriteLine($"Message Received: {messageText}", ConsoleColor.Magenta); //_p.PerformTask(module, messageText); try { var item = new QueueMessage(module, messageText); MessageWorkItem.Add(item); //MessageWorkItem.Enqueue(new QueueMessage(module,messageText)); Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine($"Added to queue: {item.ToString()}", ConsoleColor.Yellow); } catch (Exception ee) { Console.WriteLine(ee); } break; } } Console.WriteLine($"Connection Lost {module}", ConsoleColor.Red); } catch (Exception eee) { Console.WriteLine(eee); } }