Ejemplo n.º 1
0
        public void Disconnect()
        {
            // Socket.DisconnectAsync() is new in .NET 2.0 SP1
            // not available in original .NET 2.0

            SendSocketDataAvailableNotification = false;
            if (IsConnected)
            {
                try
                {
                    DisconnectDone.Reset();
                    SocketClient.BeginDisconnect(false, new AsyncCallback(DisconnectCallBack), SocketClient);
                    DisconnectDone.WaitOne();
                    SocketClient = null;
                    //if (OnEventMessage != null) { OnEventMessage(this, new EventSocketPortMessage(SocketPortMessageType.Disconnected)); }

#if FIRE_EVENT_BY_BEGININVOKE
                    if (OnEventMessage != null)
                    {
                        OnEventMessage.BeginInvoke(this, new SocketPortMessageEventArgs(SocketMessageType.Disconnected), null, null);
                    }
#else
                    if (OnEventMessage != null)
                    {
                        OnEventMessage(this, new SocketPortMessageEventArgs(SocketMessageType.Disconnected));
                    }
#endif
                }
#if DEBUG_SOCKET
                catch (Exception e)
                {
                    AccessoryLibrary.ShowException(e);
                    DisconnectDone.Set();
                }
#else
                catch (Exception ex)
                {
                    DisconnectDone.Set();
                    logger.Error(ex.ToString() + "\n");
                }
#endif
            }
        }