Beispiel #1
0
        public void Disconnect()            // replace Disconnect(), stop()
        {
            try
            {
                // cancel market data
                CancelMarketData();

                //_ibSocket.eDisconnect();
                _ibSocket.Close();
            }
            catch (Exception e)
            {
                // Relegate
                OnDebug(e.Message);
            }
        }
 public void Close()
 {
     socket.Close();
 }
Beispiel #3
0
        public void REQ_OpenSocket(string connIp, int connPort, int connClientID)
        {
            if (ClientSocket.IsConnected())
            {
                return;
            }

            Semaphore     semaphore    = new Semaphore(0, 1);
            EReaderSignal readerSignal = Signal;
            EReader       reader       = null;

            new Thread(() =>
            {
                try
                {
                    ClientSocket.eConnect(connIp, connPort, connClientID);

                    //Create a reader to consume messages from the TWS. The EReader will consume the incoming messages and put them in a queue
                    reader = new EReader(ClientSocket, readerSignal);
                    reader.Start();
                }
                catch (Exception ex)
                {
                    error(ex.Message);
                }
                finally
                {
                    semaphore.Release();
                }

                SocketConnected_Event?.Invoke(this, new SocketConnected_EventArgs(ClientSocket.IsConnected()));
            })
            {
                IsBackground = true
            }.Start();

            //Once the messages are in the queue, an additional thread can be created to fetch them
            new Thread(() =>
            {
                semaphore.WaitOne();

                try
                {
                    while (ClientSocket.IsConnected())
                    {
                        readerSignal.waitForSignal();
                        reader.processMsgs();

                        // Just for testing catch inside this loop:
                        //EReader reader2 = null;
                        //reader2.processMsgs();
                    }
                }
                catch (Exception ex)
                {
                    error(ex.Message);
                    error("A critical error occured. You need to restart the application!");
                }
                finally
                {
                    ClientSocket.Close();
                }
            })
            {
                IsBackground = true
            }.Start();
        }