private void DestroyEventQueue()
 {
     try
     {
         _eventQueue?.Dispose();
     }
     finally
     {
         _eventQueue = null;
     }
 }
        public int OpenService(string deviceClass, string deviceName, object dispatchObject)
        {
            Log.DebugFormat("OpeningService for deviceClass {0} called {1}", deviceClass, deviceName);
            OpenServiceDeviceClass = deviceClass;
            OpenServiceDeviceName  = deviceName;

            if (_props == null)
            {
                _props = GetDeviceSpecifcUposProperties();
            }

            Log.Debug("Creating EventDispatcher");
            _eventQueue = new EventThreadHelper(GetDeviceSpecificControlObjectDispatcher(dispatchObject), _props);

            Log.Debug("Getting configuration options from Registry");
            var registryValues = WindowsUposRegistry.GetRegistryValues(OpenServiceDeviceClass, OpenServiceDeviceName);

            if (VerifyDeviceSettings(registryValues))
            {
                _device     = GetDevice();
                _openResult = _device.CanCommunicateWithDevice(deviceClass, deviceName, registryValues);
                if (_openResult == OpenResultConstants.OPOS_SUCCESS)
                {
                    Log.Debug("Communication with device succeeded, Opening service is successful");
                    _props.ByName.Claimed              = false;
                    _props.ByName.DeviceEnabled        = false;
                    _props.ByName.DataEventEnabled     = false;
                    _props.ByName.FreezeEvents         = false;
                    _props.ByName.State                = ServiceStateConstants.OPOS_S_IDLE;
                    _props.ByName.DeviceDescription    = GetDeviceDescription();
                    _props.ByName.ServiceObjectVersion = GetImplementingVersion();
                    return(SetResultCode(ResultCodeConstants.Success));
                }
                Log.Error("Device Specific check for communicating with device failed. Check configuration values to verify proper values are set to be able to communicate with device");

                return(SetResultCode(ResultCodeConstants.NoHardware));
            }
            Log.Error("Device Specific validation of configuration options failed. Please verify that all settings are correct and in proper location in Windows registry");
            _openResult = OpenResultConstants.OPOS_ORS_CONFIG;

            return(SetResultCode(ResultCodeConstants.Failure));
        }