public static uint PerformReaderAction(ReaderConnectionContext ctx, Func <ReaderConnectionContext, uint> actionFunc)
 {
     if (ctx == null)
     {
         throw new ArgumentNullException("ctx");
     }
     if (actionFunc == null)
     {
         throw new ArgumentNullException("actionFunc");
     }
     try
     {
         return(actionFunc(ctx));
     }
     catch (ApiCallException exception)
     {
         return(exception.ApiCallErrorCode);
     }
     catch
     {
         throw;
     }
 }
        private void DoConnectDisconnect()
        {
            // если не подключены
            if (_currentConnectoinContext == null)
            {
                UpdateConnectBlock(false);
                try {
                    RG_ENDPOINT portEndpoint = new RG_ENDPOINT();
                    portEndpoint.Type    = _currentEndpointType;
                    portEndpoint.Address = _currentConenctionString;

                    uint errorCcode = UnmanagedContext.Instance.RG_InitDevice(ref portEndpoint, _currentReaderAddress);
                    if (errorCcode != 0)
                    {
                        logger.Error($"Error in RG_InitDevice = {errorCcode}");
                        throw new ApiCallException("Ошибка при подключении к устройству", errorCcode);
                    }
                    logger.Debug("Устройство проинициализировано");

                    RG_DEVICE_INFO_SHORT deviceInfo = new RG_DEVICE_INFO_SHORT();
                    errorCcode =
                        UnmanagedContext.Instance.RG_GetInfo(ref portEndpoint, _currentReaderAddress,
                                                             ref deviceInfo);
                    if (errorCcode != 0)
                    {
                        logger.Error($"Error in RG_GetInfo = {errorCcode}");
                        throw new ApiCallException("Ошибка при запросе данных устройства", errorCcode);
                    }

                    logger.Debug("Устройство опрошено");

                    _currentConnectoinContext =
                        new ReaderConnectionContext(portEndpoint, _currentReaderAddress, deviceInfo);

                    foreach (CodogrammData defaultCodogramm in _defaultCodogramms)
                    {
                        logger.Debug($"Пишем кодограмму {defaultCodogramm.Name}");
                        try {
                            WriteCodogramm(defaultCodogramm);
                        }
                        catch (Exception ex) {
                            MessageBox.Show(this,
                                            string.Format("({1}) {0}", ex.Message,
                                                          (ex is ApiCallException)
                                        ? (ex as ApiCallException).ApiCallErrorCode.ToString()
                                        : "..."),
                                            "Ошибка",
                                            MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                }
                catch (Exception ex) {
                    MessageBox.Show(this,
                                    string.Format("({1}) {0}", ex.Message,
                                                  (ex is ApiCallException) ? (ex as ApiCallException).ApiCallErrorCode.ToString() : "..."),
                                    "Ошибка",
                                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {
                _pollStopEvent.Set();
                _currentConnectoinContext = null;
            }

            UpdateConnectBlock(true);
            EnableDisableReaderDataGroup();
        }