/// Code to execute on Unhandled Exceptions private static void UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e) { Log.Error("ScheduledAgent.UnhandledException() \n{0}", e.ToString()); Log.Error("ScheduledAgent.UnhandledException() \n{0}", e.ToString()); if (Debugger.IsAttached) { // An unhandled exception has occurred; break into the debugger //Debugger.Break(); } }
private void SaveBusTags(BusTag[] busTags) { JsonSerializer serializer = new JsonSerializer(); serializer.NullValueHandling = NullValueHandling.Ignore; try { using (StreamWriter sw = new StreamWriter( IsolatedStorageFile.GetUserStoreForApplication().OpenFile(@"Shared\ShellContent\saved_buses.json", FileMode.OpenOrCreate, FileAccess.Write, FileShare.None))) using (JsonWriter writer = new JsonTextWriter(sw)) { serializer.Serialize(writer, busTags); } } catch (Exception ex) { Log.Error(ex.DumpStr()); } }
public static async void RefreshBusTime(ObservableCollection <BusTagVM> busTags) { var tasks = busTags.Select(b => BusTicker.GetBusDueTime(b)).ToList(); var waIdx = Enumerable.Range(0, busTags.Count).ToList(); while (tasks.Count > 0) { await Task.Run(() => { int j = Task.WaitAny(tasks.ToArray()); //Debug.WriteLine("Task.WaitAny() returns "+j); }); for (int i = tasks.Count - 1; i >= 0; --i) { if (tasks.Count == 0) { break; } if (tasks[i].IsCompleted) { int fIdx = waIdx[i]; try { busTags[fIdx].timeToArrive = tasks[i].Result; } catch (Exception ex) { Log.Error(ex.DumpStr()); busTags[fIdx].timeToArrive = "網路障礙"; } waIdx.RemoveAt(i); tasks.RemoveAt(i); } } } }
protected override void OnInvoke(ScheduledTask task) { try { //Log.Create(false); ShellTile tile = ShellTile.ActiveTiles.FirstOrDefault(x => x.NavigationUri.ToString().Contains("DefaultTitle=FromTile")); bool bWiFiOnly = false; IsolatedStorageSettings.ApplicationSettings.TryGetValue("WiFiOnly", out bWiFiOnly); if ((bWiFiOnly && !WifiConnected()) || tile == null) { ScheduledActionService.LaunchForTest(task.Name, TimeSpan.FromSeconds(1)); this.NotifyComplete(); return; } var busTags = new ObservableCollection <BusTagVM>( from bus in LoadBusTags() orderby bus.tag select new BusTagVM(bus)); Log.Debug("busTags=" + busTags.DumpArray()); RefreshBusTime(busTags); //var tasks = busTags.Select(b => BusTicker.GetBusDueTime(b)).ToList(); //var waIdx = Enumerable.Range(0, busTags.Length).ToList(); //while (tasks.Count > 0) //{ // Task.WaitAny(tasks.ToArray()); // for (int i = tasks.Count - 1; i >= 0; --i) // { // if (tasks.Count == 0) // break; // if (tasks[i].IsCompleted) // { // int fIdx = waIdx[i]; // try // { // busTags[fIdx].timeToArrive = tasks[i].Result; // } // catch (Exception ex) // { // Log.Error(ex.DumpStr()); // busTags[fIdx].timeToArrive = "網路障礙"; // } // waIdx.RemoveAt(i); // tasks.RemoveAt(i); // } // } //} SaveBusTags(busTags.Select(x => x.BusTag).ToArray()); Deployment.Current.Dispatcher.BeginInvoke(() => { Log.Debug("Deployment.Current.Dispatcher.BeginInvoke enter"); try { Log.Debug("UpdateTileImage()"); UpdateTileImage(busTags.Select(x => x.BusTag).ToArray()); Log.Debug("Deployment.Current.Dispatcher.BeginInvoke exit"); } catch (Exception e) { Log.Error(e.ToString()); } finally { ScheduledActionService.LaunchForTest(task.Name, TimeSpan.FromSeconds(1)); this.NotifyComplete(); //Log.Close(); } }); } catch (Exception e) { Log.Error(e.DumpStr()); } }