static void Sender(string queuePath) { using (var queueConnectionPool = new QueueConnectionPool()) using (var mqTransaction = new MessageQueueTransaction()) { var queueConnection = queueConnectionPool.Create(queuePath); queueConnection.CreateIfNotExists(true); mqTransaction.Begin(); foreach (var item in Enumerable.Range(1, 1000)) { Console.WriteLine($"Add Data: {item}/1000"); var model = new DataModel { Name = $"Data - {item}" }; queueConnection.Send(model, MessagePriority.Normal, mqTransaction); } mqTransaction.Commit(); } }
static void Receiver(string queuePath) { SemaphoreSlim semaphoreSlim = new SemaphoreSlim(Environment.ProcessorCount); using (var queueConnectionPool = new QueueConnectionPool()) { while (true) { var queueConnection = queueConnectionPool.Create(queuePath); var model = queueConnection.Receive <DataModel>(); semaphoreSlim.Wait(); ThreadPool.QueueUserWorkItem((state) => { int count = queueConnection.Count(); Console.WriteLine($"當前: {model.Name}, 剩餘: {count}"); Thread.Sleep(1000); semaphoreSlim.Release(); }); } } }