private void RefreshLists()
 {
     lock (SerialPortToken.Instance)
     {
         _gateways = _gatewayService.GetGateways();
         uxGateways.Items.Clear();
         uxGateways.Items.AddRange(_gateways.Where(g => g.IsAvailable).ToArray());
         if (uxGateways.Items.Count <= 0)
         {
             return;
         }
         uxGateways.SelectedIndex = 0;
     }
 }
        private void GetGateways()
        {
            DisplayMessage("Pobranie gatewayow");
            uxSerialPortList.Items.Clear();
            uxSerialPortStatus.Items.Clear();
            _gateways.Clear();

            _gateways = _gatewayService.GetGateways();

            foreach (var gateway in _gateways)
            {
                lock (SerialPortToken.Instance)
                {
                    try
                    {
                        ReloadSerialPortListAndStatus(gateway);
                        if (!gateway.IsAvailable)
                        {
                            continue;
                        }
                        var response = _gatewayService.GetResponse(gateway);
                        _gatewayRepository.InsertGatewayResponse(gateway, response);
                    }
                    catch (NpgsqlException ex)
                    {
                        if (_isSendEmail)
                        {
                            continue;
                        }
                        try
                        {
                            IncreaseErrorCounter();
                            DisplayMessage(ex.Message);
                            _emailService.SendEmail("Błąd połączenia z bazą danych", ex.Message);
                            _isSendEmail = true;
                        }
                        catch (Exception innerEx)
                        {
                            DisplayMessage(innerEx.Message);
                        }
                    }
                    catch (Exception ex)
                    {
                        IncreaseErrorCounter();
                        DisplayMessage(ex.Message);
                    }
                }
            }
        }