private void ReaderThread(object stateInfo)
 {
     // Checks if there is something to read.
     this.IntercomStatus = IpcMMFinterComSF.MMFinterComTStatus.TryingToRead;
     this.reading        = accessor.ReadBoolean(ReadPosition + DATA_AVAILABLE_OFFSET);
     if (this.reading)
     {
         this.IntercomStatus = IpcMMFinterComSF.MMFinterComTStatus.ReadingData;
         // Checks how many bytes to read.
         int availableBytes = accessor.ReadInt32(ReadPosition + DATA_LENGTH_OFFSET);
         this.ReadData = new byte[availableBytes];
         // Reads the byte array.
         int read = accessor.ReadArray <byte>(ReadPosition + DATA_OFFSET, this.ReadData, 0, availableBytes);
         // Sets the flag used to signal that there aren't available data anymore.
         accessor.Write(ReadPosition + DATA_AVAILABLE_OFFSET, false);
         // Sets the flag used to signal that data has been read.
         accessor.Write(ReadPosition + READ_CONFIRM_OFFSET, true);
         this.IntercomStatus = IpcMMFinterComSF.MMFinterComTStatus.FinishedReading;
     }
     else
     {
         this.IntercomStatus = IpcMMFinterComSF.MMFinterComTStatus._Null;
     }
 }
        protected override void PerformMainLoopService()
        {
            int    pid  = 0;
            string mesg = null;

            bool dataReady = isUsable && DBWIN_DATA_READY.WaitOne(TimeSpan.Zero);

            if (dataReady)
            {
                pid = DBWIN_BUFFER_Accessor.ReadInt32(0);
                int readMesgBytes = DBWIN_BUFFER_Accessor.ReadArray(4, mesgByteBuffer, 0, bufferSize);

                int len = bufferSize;

                for (int idx = 0; idx < len; idx++)
                {
                    if (mesgByteBuffer[idx] == 0)
                    {
                        len = idx;
                    }
                }

                mesg = ByteArrayTranscoders.ByteStringTranscoder.Encode(mesgByteBuffer, 0, len);
                mesgByteBuffer.Clear();

                DBWIN_BUFFER_Accessor.WriteArray(0, zeroBuffer, 0, zeroBuffer.Length);

                DBWIN_DATA_READY.Reset();
                DBWIN_BUFFER_READY.Set();
            }

            if (BaseState.IsOnline && pid != 0 && !mesg.IsNullOrEmpty())
            {
                Func <System.Diagnostics.Process> func           = (() => System.Diagnostics.Process.GetProcessById(pid));
                System.Diagnostics.Process        sendingProcess = func.TryGet();

                Tuple <Logging.ILogger, Logging.IMesgEmitter> pidTuple = null;

                if (tupleDictionary.TryGetValue(pid, out pidTuple) && pidTuple != null)
                {
                    if (sendingProcess != null && !pidTuple.Item1.Name.EndsWith(sendingProcess.ProcessName))
                    {
                        pidTuple = null;
                    }
                }

                if (pidTuple == null)
                {
                    string loggerName;
                    if (sendingProcess != null)
                    {
                        loggerName = "{0}.pid{1}.{2}".CheckedFormat(PartID, pid, sendingProcess.ProcessName);
                    }
                    else
                    {
                        loggerName = "{0}.pid{1}".CheckedFormat(PartID, pid);
                    }

                    Logging.ILogger      logger  = new Logging.Logger(loggerName);
                    Logging.IMesgEmitter emitter = logger.Emitter(generateMesgType);

                    pidTuple             = Tuple.Create(logger, emitter);
                    tupleDictionary[pid] = pidTuple;
                }

                if (pidTuple != null)
                {
                    pidTuple.Item2.Emit("{0}", mesg.TrimEnd(' ', '\t', '\r', '\n'));
                }
            }
        }