static void DemoSendNormalMessages() { PrintTitle("Demo: Sending 10 Normal Messages"); InterprocessCommunication comA = new InterprocessCommunication(); InterprocessCommunication comB = new InterprocessCommunication(); Console.Write("\r\n Initializing IPC on 'CommunicationA'"); if (!comA.Initialize("CommunicationA")) { Console.Write("\r\n IPC on 'CommunicationA' failed.\r\n Last Error: {0}", comA.LastError); return; } Console.Write("\r\n Successfully initialized IPC on 'CommunicationA'"); Console.Write("\r\n Initializing IPC on 'CommunicationB'"); if (!comB.Initialize("CommunicationB")) { Console.Write("\r\n IPC on 'CommunicationB' failed.\r\n Last Error: {0}", comB.LastError); return; } Console.Write("\r\n Successfully initialized IPC on 'CommunicationB'"); Console.Write("\r\n\r\n Sending 5 messages from 'CommunicationA' to 'CommunicationB' and vice versa"); for (int i = 0; i < 5; i++) { comA.PostMessage("CommunicationB", "Command", "Message #" + (i + 1)); comB.PostMessage("CommunicationA", "Command", "Message #" + (i + 1)); } IPCMessage msg; for (int i = 0; i < 5; i++) { msg = IPCMessage.FromString(comA.GetMessage()); msg.Print(); msg = IPCMessage.FromString(comB.GetMessage()); msg.Print(); } Console.Write("\r\n\r\n Closing 'CommunicationA' ... "); comA.Dispose(); Console.Write("Closed"); Console.Write("\r\n Closing 'CommunicationB' ... "); comB.Dispose(); Console.Write("Closed"); Console.Write("\r\n\r\n Sending 10 normal messages demonstration successfully finished."); PrintTerminatingTitle(); }
static void DemoPerformance(int MessageCount, int BodySize, ushort DisplayMsgDegree = 0, ushort DisplayOverallDegree = 0) { PrintDemoPerformanceTitle(MessageCount, BodySize, DisplayMsgDegree, DisplayOverallDegree); InterprocessCommunication comA = new InterprocessCommunication(); InterprocessCommunication comB = new InterprocessCommunication(); DateTime start, end; Console.Write("\r\n IPC on 'CommunicationB' will be receiving {0} messages with bodies of size: {1}", MessageCount, BodySize); uint pageSize = (uint)BodySize + 80; pageSize = 4096 * (pageSize / 4096 + 1); Console.Write("\r\n Setting the message queue size in 'CommunicationB' to {0} (equivalent to {1} Windows Pages)", pageSize, pageSize / 4096); comB.SetMaximumMessageSize(pageSize); Console.Write("\r\n Initializing IPC on 'CommunicationA'"); if (!comA.Initialize("CommunicationA")) { Console.Write("\r\n IPC on 'CommunicationA' failed.\r\n Last Error: {0}", comA.LastError); return; } Console.Write("\r\n Successfully initialized IPC on 'CommunicationA'"); Console.Write("\r\n Initializing IPC on 'CommunicationB'"); if (!comB.Initialize("CommunicationB")) { Console.Write("\r\n IPC on 'CommunicationB' failed.\r\n Last Error: {0}", comB.LastError); return; } Console.Write("\r\n Successfully initialized IPC on 'CommunicationB'"); comA.WaitPeriod = 1000; comB.WaitPeriod = 1000; StringBuilder str = new StringBuilder(); for (int i = 0; i < BodySize; i++) { str.Append("A"); } string msgBody = str.ToString(); start = DateTime.Now; Console.Write("\r\n\r\n {0} - Sending {1} msgs. Body size: '{2}'. From 'A' to 'B'", start.ToString("[MM/dd/yyyy HH:mm:ss.fff]"), MessageCount, BodySize); for (int i = 0; i < MessageCount; i++) { comA.PostMessage("CommunicationB", "Performance", msgBody); } SpinWait.SpinUntil(new Func <bool>(() => { return(comA.TotalSent >= MessageCount); })); end = DateTime.Now; Console.Write("\r\n {0} - Sent {1} msgs. Body size: '{2}'. From 'A' to 'B'", end.ToString("[MM/dd/yyyy HH:mm:ss.fff]"), MessageCount, BodySize); Console.Write("\r\n\r\n Total time to send {0} messages: {1} seconds", MessageCount, (end - start).TotalSeconds); Console.Write("\r\n Average time per message: {0} microseconds", (end.Ticks - start.Ticks) / (10 * MessageCount)); long totalSizeinKB = MessageCount * (80 + BodySize) / 1024; Console.Write("\r\n Average time per 1KB: {0} microseconds", (end.Ticks - start.Ticks) / (10 * totalSizeinKB)); Console.Write("\r\n\r\n Closing 'CommunicationA' ... "); comA.Dispose(); Console.Write("Closed"); Console.Write("\r\n Closing 'CommunicationB' ... "); comB.Dispose(); Console.Write("Closed"); Console.Write("\r\n\r\n Demonstration successfully finished."); PrintTerminatingTitle(); GC.Collect(); Thread.Sleep(1000); }