private void TesterDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e) { Tester tester = testerDataGrid.SelectedItem as Tester; Trainee trainee = GetTrainee(); AddressStruct testAddress = GetTestAddress(); if (tester == null || testAddress == null || testAddress == new AddressStruct() || trainee == null) { return; } selectTesterButton.IsEnabled = true; double distance = BLTools.GetDistanceFromDataSource(trainee.Address, tester.Address); //If no distance was already measured between 2 addresses if (distance < 0) { Task.Run(() => { distance = BLTools.CalculateDistanceGoogle(trainee.Address, tester.Address); }).ContinueWith((t) => { Dispatcher.Invoke(() => { testerDistanceTb.Text = distance.ToString("0.00") + " Km"; }); }); } //If distance was already measured between 2 addresses else { testerDistanceTb.Text = distance.ToString("0.00") + " Km"; } }
private async void FindClosetTesterButton_Click(object sender, RoutedEventArgs e) { distanceSpinner.Visibility = Visibility.Visible; distanceSpinner.Spin = true; m_targetTrainee = GetTrainee(); Tester tester = new Tester(); await Task.Run(async() => { tester = BLTools.GetClosestTester(m_targetTrainee.Address); if (tester == null) { return; } double distance = BLTools.GetDistanceFromDataSource(m_targetTrainee.Address, tester.Address); //If distance was NOT found in the DataSource if (distance == -1) { m_distance = BLTools.CalculateDistanceGoogle(m_targetTrainee.Address, tester.Address); } //If distance WAS found in the DataSource else { m_distance = distance; } }).ContinueWith((str) => { Dispatcher.Invoke(() => { //If no internet connection was found if (m_distance == -2) { MessageBox.Show("No internet connection found"); return; } if (tester == null) { distanceTb.Text = "f"; distanceTb.Text = ""; MessageBox.Show(string.Format("Couldn't find a tester"), "Error!", MessageBoxButton.OK, MessageBoxImage.Error); return; } if (m_distance != 100.00001 && m_distance != -1) { distanceTb.Text = ""; distanceTb.Text = m_distance.ToString("0.00") + " Km"; testCityCb.ComboBox.SelectedItem = m_targetTrainee.Address.City; testStreetCb.ComboBox.SelectedItem = m_targetTrainee.Address.Street; m_Ibl.AddDistance(new DistanceStruct(m_targetTrainee.Address, tester.Address, m_distance)); TestersInRange = new List <Tester>(); TestersInRange.Add(tester); if (TestersInRange.Count == 0) { MessageBox.Show(string.Format("Couldn't find a tester"), "Error!", MessageBoxButton.OK, MessageBoxImage.Error); } else { MessageBox.Show(string.Format("Success!\nPlease choose the tester to continue"), "Success!", MessageBoxButton.OK, MessageBoxImage.Information); } } FilterData(); }); }); }