private void btnStart_Click(object sender, RoutedEventArgs e) { Task.Run(async() => { bool isOpened = await this.tmp102.OpenAsync(); IDictionary bag = new Dictionary <SensorType, float>(); while (true) { float temperature = tmp102.Temperature(); SensorType sensorType = SensorType.Temperature; if (!bag.Contains(sensorType)) { bag.Add(sensorType, temperature); } else { bag[sensorType] = temperature; } if ((this.iotClient != null) && (this.iotClient.IsOpen)) { this.iotClient.SendAsync(bag); } await Task.Delay(5000); } }); }
public async void Run(IBackgroundTaskInstance taskInstance) { // // TODO: Insert code to start one or more asynchronous methods // BackgroundTaskDeferral deferral = taskInstance.GetDeferral(); this.tmp102 = new TMP102(); // set and open the IoT client if (this.iotClient == null) { //this.iotClient = new IoTClient("raspberrypi2", Guid.NewGuid().ToString(), this.connectionString, this.eventHubEntity); this.iotClient = new IoTClientConnectTheDots("raspberrypi2", Guid.NewGuid().ToString(), this.connectionString, this.eventHubEntity); } if (!this.iotClient.IsOpen) { this.iotClient.Open(); } bool isOpened = await this.tmp102.OpenAsync(); IDictionary bag = new Dictionary <SensorType, float>(); while (true) { float temperature = tmp102.Temperature(); SensorType sensorType = SensorType.Temperature; if (!bag.Contains(sensorType)) { bag.Add(sensorType, temperature); } else { bag[sensorType] = temperature; } if ((this.iotClient != null) && (this.iotClient.IsOpen)) { this.iotClient.SendAsync(bag); } await Task.Delay(5000); } deferral.Complete(); }
public async void Run(IBackgroundTaskInstance taskInstance) { var deferral = taskInstance.GetDeferral(); try { TMP102 temp = new TMP102(A0AddressSelect.GND); Task init = temp.OpenAsync(); init.Wait(); while (true) { float degree = temp.Temperature(); Debug.WriteLine(degree); await Task.Delay(500); } } finally { deferral.Complete(); } }