/* Connects to the proxy and then calls the callback. Blocking function */ public bool connectToProxy(string ip, int port, ConnectionAttemptCallback callback) { ConnectionAttemptCallback realCallback = null; realCallback = delegate(bool success) { callback(success); ConnectionAttemptEvent -= realCallback; }; ConnectionAttemptEvent += realCallback; bool res = connectToProxy(ip, port); if (ConnectionAttemptEvent != null) { ConnectionAttemptEvent(res); } return(res); }
/* Connects to the proxy in a new thread and then calls the callback from this thread. Non-Blocking function */ public void connectToProxyAsync(string ip, int port, ConnectionAttemptCallback callback) { ConnectionAttemptCallback realCallback = null; realCallback = delegate(bool success) { callback(success); ConnectionAttemptEvent -= realCallback; }; ConnectionAttemptEvent += realCallback; BackgroundWorker bw = new BackgroundWorker(); bw.DoWork += new DoWorkEventHandler( delegate(object o, DoWorkEventArgs args) { bool res = connectToProxy(ip, port); if (ConnectionAttemptEvent != null) { ConnectionAttemptEvent(res); } }); bw.RunWorkerAsync(); }