Exemplo n.º 1
0
        private void ListenThread()
        {
            while (listen)
            {
                byte[] buffer = new byte[Marshal.SizeOf(typeof(LineInfo))];

                if (!client.IsConnected)
                {
                    Connect(pid);
                }



                try
                {
                    bs.Read(buffer, 0, Marshal.SizeOf(typeof(LineInfo)));
                }
                catch (ObjectDisposedException ex)
                {
                    try
                    {
                        Connect(pid);
                    }
                    catch (Exception e)
                    {
                        ErrorLog.OnError(e);
                    }
                }
                catch (InvalidOperationException ex)
                {
                    try
                    {
                        Connect(pid);
                    }
                    catch (Exception e)
                    {
                        ErrorLog.OnError(e);
                    }
                }
                catch (Exception ex)
                {
                    ErrorLog.OnError(ex);
                }

                GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
                LineInfo li;

                try
                {
                    li = (LineInfo)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(LineInfo));
                }
                finally
                {
                    handle.Free();
                }

                string s = li.Text;
                if (!string.IsNullOrEmpty(s))
                {
                    s = CleanLine(s.TrimEnd('\0'));
                    NewLine(this, new NewLineEventArgs(s));
                }
            }
        }