/* Disconnects from the proxy if connected, then calls the callback. Blocking */ public void disconnect(DisconnectionCallback callback) { if (server.Connected) { DisconnectionCallback realCallback = null; realCallback = delegate() { callback(); DisconnectionEvent -= realCallback; }; DisconnectionEvent += realCallback; send((byte)COMMANDS.DISCONNECT, 0); server.Shutdown(SocketShutdown.Both); server.Close(); PRINT("Disconnected from server!"); if (DisconnectionEvent != null) { DisconnectionEvent(); } } }
/* Disconnects from the proxy in a new thread if connected, then calls the callback from this thread. Non-Blocking function */ public void disconnectAsync(DisconnectionCallback callback) { DisconnectionCallback realCallback = null; realCallback = delegate() { callback(); DisconnectionEvent -= realCallback; }; DisconnectionEvent += realCallback; BackgroundWorker bw = new BackgroundWorker(); bw.DoWork += new DoWorkEventHandler( delegate(object o, DoWorkEventArgs args) { disconnect(); if (DisconnectionEvent != null) { DisconnectionEvent(); } }); bw.RunWorkerAsync(); }