private async void Button_Click_Device(object sender, RoutedEventArgs e) { await Task.Run(() => { try { if (DeviceBasicMode) { DeviceStream_Device.RunDevice(device_cs, OnDeviceRecvTextIO).GetAwaiter().GetResult(); } if (!DeviceUseCustomClass) { DeviceStream_Device.RunDevice(device_cs, OnDeviceRecvTextIO, OnDeviceStatusUpdate, ActionCommand, AppSettingsValues.Settings.KeepDeviceListening).GetAwaiter().GetResult(); } else { DeviceStream_Device.RunDevice(device_cs, OnDeviceRecvTextIO, OnDeviceStatusUpdate, ActionCommand, AppSettingsValues.Settings.KeepDeviceListening, new DeviceSvcCurrentSettings_Example()).GetAwaiter().GetResult(); } } catch (TaskCanceledException) { System.Diagnostics.Debug.WriteLine("0 Error App.RunClient(): Task cancelled"); } catch (OperationCanceledException) { System.Diagnostics.Debug.WriteLine("0 Error App.RunClient(): Operation cancelled"); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("0 Error App.RunClient(): " + ex.Message); } }); }
private static void RunDevice(string device_cs, double ts) { DeviceStreamingCommon.DeviceTimeout = TimeSpan.FromSeconds(ts); try { if (Settings._Settings.basicMode) { DeviceStream_Device.RunDevice(device_cs, OnDeviceRecvTextIO).GetAwaiter().GetResult(); } else if (!Settings._Settings.UseCustomClass) { DeviceStream_Device.RunDevice(device_cs, OnDeviceRecvTextIO, OnDeviceStatusUpdate, ActionCommand, Settings._Settings.KeepDeviceListening).GetAwaiter().GetResult(); } else { DeviceStream_Device.RunDevice(device_cs, OnDeviceRecvTextIO, OnDeviceStatusUpdate, ActionCommand, Settings._Settings.KeepDeviceListening, new DeviceSvcCurrentSettings_Example()).GetAwaiter().GetResult(); } } //catch (Microsoft.Azure.Devices.Client.Exceptions.IotHubCommunicationException) //{ // System.Diagnostics.Debug.WriteLine("0 Error App.RunClient(): Hub connection failure"); //} //catch (Microsoft.Azure.Devices.Common.Exceptions.DeviceNotFoundException) //{ // System.Diagnostics.Debug.WriteLine("0 Error App.RunClient(): Device not found"); //} catch (TaskCanceledException) { System.Diagnostics.Debug.WriteLine("0 Error App.RunClient(): Task canceled"); } catch (OperationCanceledException) { System.Diagnostics.Debug.WriteLine("0 Error App.RunClient(): Operation canceled"); } catch (Exception ex) { if (ex.Message.Contains("Timeout")) { System.Diagnostics.Debug.WriteLine("0 Error App.RunClient(): " + ex.Message); } else { System.Diagnostics.Debug.WriteLine("0 Error App.RunClient(): Timeout"); } } }