Пример #1
0
        public ClientGui()
        {
            bool success = false;
            InitializeComponent();
            _graph = new Graph();
            _graph.Initialize_Checkboxes_Client();
            _graph.InitializeChart_Client();
            _graph.InitializeGraph();
            AddGraphToForm();

            this.FormClosing += on_Window_Closed_Event;
            setVisibility(false);

            // update waarden met de data van de fiets
            _updateDataTimer = new Timer();
            _updateDataTimer.Interval = 1000;
            _updateDataTimer.Tick += UpdateGUI;

            // timer voor het verwijderen van de errortekst, 'cosmetisch'
            _labelRemoveTimer = new Timer();
            _labelRemoveTimer.Interval = 3000;
            _labelRemoveTimer.Tick += UpdateLabel;

            if(SIM_ON)
            {
                Connect("SIM");
            }
            else
            {
                Connect("");
            }

            _bikeController.LockPower();

            try
            {
                //opzetten tcp connectie
                TcpClient TcpClient = new TcpClient(_server, _port);
                _client = new ClientTcpConnector(TcpClient, _server);

                new System.Threading.Thread(() =>
                {
                    while (true)
                    {
                        if (_userIsAuthenticated)
                        {
                            Packet packet = null;
                            if (_client.isConnected() && !this.IsDisposed)
                            {
                                //Console.WriteLine("Reading message\n");
                                packet = _client.ReadMessage();

                                if (packet != null)
                                {
                                    processPacket(packet);
                                }
                            }
                        }
                        System.Threading.Thread.Sleep(5);
                    }
                }).Start();
                success = true;

            }
            catch (SocketException)
            {
                MessageBox.Show("Could not connect to the server, trying to reconnect");
                success = false;
            }
            catch (TimeoutException)
            {
                MessageBox.Show("Lost connection to the server (timed out), trying to reconnect");
                success = false;
            }
            catch (Exception e)
            {
                MessageBox.Show("An error occured: " + e.Message);
                success = false;
            }
                //rewrite this to make the the reconnection
            finally
            {
                if (!success)
                {
                    //opzetten tcp connectie
                    TcpClient TcpClient = new TcpClient(_server, _port);
                    _client = new ClientTcpConnector(TcpClient, _server);

                    new System.Threading.Thread(() =>
                    {
                        while (true)
                        {
                            if (_userIsAuthenticated)
                            {
                                Packet packet = null;
                                if (_client.isConnected() && !this.IsDisposed)
                                {
                                    //Console.WriteLine("Reading message\n");
                                    packet = _client.ReadMessage();

                                    if (packet != null)
                                    {
                                        processPacket(packet);
                                    }
                                }
                            }
                            System.Threading.Thread.Sleep(5);
                        }
                    }).Start();
                }
            }
        }