private void SearchCompleted(object operationState) { DeviceSearchCompletedEventArgs e = operationState as DeviceSearchCompletedEventArgs; if (this.DeviceSearchCompleted != null) { this.DeviceSearchCompleted(this, e); } }
/// <summary> /// Запускает поиск устройства /// </summary> public void StartSearch() { lock (this.requestingPorts) { if (this.requestingPorts.Count != 0) { throw new Exception("Поиск устройства уже запущен, необходимо дождаться завершения"); } } this.deviceResponse = null; this.canceled = false; if (devicePort != null) { byte[] res = { }; try { devicePort.Open(); res = devicePort.SendRequest(this.Request, this.Pattern.Length); if (res != null && (Encoding.ASCII.GetString(res).ToLower().Contains(this.Pattern.ToLower()) || Encoding.ASCII.GetString(res).ToLower().Contains(this.BootPattern.ToLower()))) { this.deviceResponse = res; } } catch { } finally { devicePort.Close(); } } if (deviceResponse != null) { DeviceSearchCompletedEventArgs e = new DeviceSearchCompletedEventArgs(this.devicePort, this.deviceResponse, null, canceled); if (this.DeviceSearchCompleted != null) { this.DeviceSearchCompleted(this, e); } } else { this.devicePort = null; this.progress = 0; this.portsCount = 0; string[] portNames = SerialPort.GetPortNames(); this.portsCount = portNames.Length; foreach (string portName in portNames) { if (!this.requestingPorts.Keys.Contains(portName)) { WorkerEventHandler workerDelegate = new WorkerEventHandler(this.SearchDevice); AsyncOperation asyncOp = AsyncOperationManager.CreateOperation(portName); this.requestingPorts.Add(portName, asyncOp); workerDelegate.BeginInvoke(portName, asyncOp, null, null); } } } }
private void CompletionMethod(SerialportChannel port, byte[] response, Exception exception, bool canceled, AsyncOperation asyncOp) { DeviceSearchCompletedEventArgs e = new DeviceSearchCompletedEventArgs(port, response, exception, canceled); asyncOp.PostOperationCompleted(onCompletedDelegate, e); }