/// <summary> /// BlackBerry 设备USB端口监听预处理 /// 1,初始化服务(传入服务名称初始化目录,绝对路径包括服务名称) /// 2,开始监听 /// 说明,1步骤必须先于2步骤。 /// </summary> public void Start() { // 服务启动失败:是否进行过重试 int doretry = 0; DoRetry: try { // 默认以安装目录作为初始化目录,反之服务启动失败则以指定目录作为初始化目录 string str_USBMonitorService_Init_Path = string.Empty; if (doretry == 0) { str_USBMonitorService_Init_Path = USBServiceName_Install_Path; } else { str_USBMonitorService_Init_Path = USBMonitorService_Init_Path(); } // 获取初始化目录(Unicode),用于初始化服务传入 byte[] byte_USBMonitorService_Init_Path = Encoding.Unicode.GetBytes(str_USBMonitorService_Init_Path); // 1,初始化服务(传入服务名称初始化目录,绝对路径包括服务名称) int result = USBMonitorService.USBMonitor_Service_Init(byte_USBMonitorService_Init_Path); if (result != 0) { LoggerManagerSingle.Instance.Error(string.Format("设备USB端口监听预处理:初始化服务失败, 错误编码{0}", result)); throw new ApplicationException(string.Format("设备USB端口监听预处理:初始化服务失败, 错误编码{0}", result)); } // 2,开始监听 result = USBMonitorService.USBMonitor_Start(this._USBDeviceMonitorCallback); if (result != 0) { LoggerManagerSingle.Instance.Error(string.Format("设备USB端口监听预处理:监听启动失败, 错误编码{0}", result)); throw new ApplicationException(string.Format("设备USB端口监听预处理:监听启动失败, 错误编码{0}", result)); } } catch (Exception ex) { if (doretry == 0) { this.Close(); doretry = 1; goto DoRetry; } LoggerManagerSingle.Instance.Error(ex, "设备USB端口监听预处理异常"); } }
/// <summary> /// 关闭设备监听,释放资源。 /// 1,停止监听 /// 2,停止并删除服务 /// 3,删除初始化目录 /// </summary> public void Close() { int m1 = 0; int m2 = 0; m1 = USBMonitorService.USBMonitor_Stop(); try { m1 = USBMonitorService.USBMonitor_Stop(); m2 = USBMonitorService.USBMonitor_Service_Del(); } catch (Exception ex) { LoggerManagerSingle.Instance.Error(ex); } FileHelper.DeleteDirectory(USBService_Init_Dir); }