Ejemplo n.º 1
0
        public void ReadPipeThread(object state)
        {
            NamedPipeServerStream PipeStream = state as NamedPipeServerStream;

            while (true)
            {
                BufPipe = new byte[BufferSize];
                AsyncState asyncState = new AsyncState()
                {
                    Buffer    = BufPipe,
                    EvtHandle = new ManualResetEvent(false),
                    Stream    = pipeStream
                };
                pipeStream.BeginRead(BufPipe, 0, BufPipe.Length, new AsyncCallback(ReadCallback), asyncState);
                asyncState.EvtHandle.WaitOne();
            }
        }
Ejemplo n.º 2
0
        private void ReadCallback(IAsyncResult arg)
        {
            AsyncState state       = arg.AsyncState as AsyncState;
            int        length      = state.Stream.EndRead(arg);
            string     readContent = "";

            if (length == 0)
            {
                // MainForm._mainForm.Invoke(new MainPictureHandler(MainForm._mainForm.MainPictureOFF));
                ShowPipeLedOffRun();
                pipeStream.Dispose();
                return;
            }
            if (length > 0)
            {
                byte[] buffer;
                if (length == BufferSize)
                {
                    buffer = state.Buffer;
                }
                else
                {
                    buffer = new byte[length];
                    Array.Copy(state.Buffer, 0, buffer, 0, length);
                    readContent = Encoding.UTF8.GetString(buffer);
                    // MessageBox.Show(readContent);
                }
                // if (state.MemoryStream == null) state.MemoryStream = new MemoryStream();
                //  state.MemoryStream.Write(buffer, 0, buffer.Length);
                //  state.MemoryStream.Flush();
            }
            if (length < BufferSize)
            {
                /*
                 * state.MemoryStream.Position = 0;
                 * using (StreamReader reader = new StreamReader(state.MemoryStream))
                 * {
                 *  this.InputStr.Append(reader.ReadToEnd());
                 *
                 * }
                 * state.MemoryStream.Close();
                 * state.MemoryStream.Dispose();
                 * state.MemoryStream = null;
                 */

                ReadFPTRun(readContent);

                /*
                 * if(MainForm.blpipe)
                 * {
                 *  if (readContent.CompareTo("TESTOK") == 0)
                 *  {
                 *     if (1 == NPserialPortComm.WriteMultiSinglePoint("%%03#WCSR1011**\r"))
                 *       MessageBox.Show("Send TESTOK Fail");
                 *  }
                 *  if (readContent.CompareTo("TESTNG") == 0)
                 *  {
                 *     if (1 == NPserialPortComm.WriteMultiSinglePoint("%%03#WCSR1021**\r"))
                 *       MessageBox.Show("Send TESTNG Fail");
                 *  }
                 * }  */
                state.EvtHandle.Set();
            }
            else
            {
                Array.Clear(state.Buffer, 0, BufferSize);
                //再次执行异步读取操作
                state.Stream.BeginRead(state.Buffer, 0, BufferSize, new AsyncCallback(ReadCallback), state);
            }
        }