Note that there are some explanatory texts on larger screens.

plurals
  1. POBackground task is skipping schedule, updating every 60 min sometimes
    primarykey
    data
    text
    <p>I'm about to loose it soon... I have two apps with working background tasks that are updating the live tile. The data for the live tile is downloaded, parsed and then an image is created dynamically and used as the background for the live tile.</p> <p>Everything is working just fine for a day or two, but then the updating starts behaving very strange. The first one-two days both live tiles for my apps are updating every 28 minutes like clockwork. But then they start skipping updates. Often app A then updates when app B doesn't update the live tile so that they are not updating at the same time and only once an hour. To put it simple they are way off schedule.</p> <p>This is really frustrating since I need to be able to rely on the tiles beeing updated every 30 minutes (if I have enough battery, good reception and so on).</p> <p>I would really appreciate if someone could help me out and maybe take a look at my code to see if there might be something messing up the update interval (like not calling NotifyComplete correctly). I have removed some code and and tried to simplify it. Please ask if you need anything else to understand this.</p> <p>I have been trying to fix this for the last two months, trying different phones and going throughmy code very carefully.</p> <p>Your help is more appreciated than you can ever know. Thanks in advance.</p> <pre><code>My OnInvoke function: Timer t = null; ShellToast toast = new ShellToast(); try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(strUrlHBH)); request.Accept = "*/*"; request.AllowAutoRedirect = true; // disable caching. request.Headers["Cache-Control"] = "no-cache"; request.Headers["Pragma"] = "no-cache"; t = new Timer( state =&gt; { if (string.Compare(state.ToString(), id, StringComparison.InvariantCultureIgnoreCase) == 0) { //logger.Write("Timeout reached for connection [{0}], aborting download.", id); runNotifyComplete = false; NotifyComplete(); request.Abort(); t.Dispose(); } }, id, timeout, 0); request.BeginGetResponse( r =&gt; { try { if (t != null) { t.Dispose(); } var httpRequest = (HttpWebRequest)r.AsyncState; var httpResponse = (HttpWebResponse)httpRequest.EndGetResponse(r); using (var reader = new StreamReader(httpResponse.GetResponseStream())) { var response = reader.ReadToEnd(); Deployment.Current.Dispatcher.BeginInvoke(new Action(() =&gt; { try { //Parse the result if (boolResult) //If we have a result { Grid grid = new Grid(); StackPanel sp = new StackPanel(); sp.Height = 173; sp.Width = 173; //StreamResourceInfo info = Application.GetResourceStream(new Uri("Pin-to-start2.png", UriKind.Relative)); if ((bool)settings["LiveTileMetro"] == true) { // create source bitmap for Image control (image is assumed to be alread 173x173) /*WriteableBitmap wbmp2 = new WriteableBitmap(1, 1); wbmp2.SetSource(info.Stream); Image img = new Image(); img.Source = wbmp2; // add Image to Grid grid.Children.Add(img); strBackBackground = "Pin-to-start2.png"; } else {*/ sp.Background = (SolidColorBrush)Application.Current.Resources["PhoneAccentBrush"]; //sp.Background.Opacity = 0.0; strBackBackground = ""; } StreamResourceInfo info; //GC.Collect(); info = Application.GetResourceStream(new Uri("/MyApp;component/images/Icons/livetile/livetile.png", UriKind.Relative)); WriteableBitmap wbmp3 = new WriteableBitmap(1, 1); try { wbmp3.SetSource(info.Stream); } catch { } Image img3 = new Image(); img3.Source = wbmp3; // add Image to Grid img3.Width = 173; img3.Height = 173; img3.Margin = new Thickness { Left = 0, Bottom = 0, Right = 0, Top = 0 }; TextBlock txtTemperature = new TextBlock(); TextBlock txtTemperatureRing = new TextBlock(); txtTemperature.Foreground = new SolidColorBrush(Colors.White); txtTemperature.Text = strTemp; txtTemperature.TextAlignment = TextAlignment.Right; txtTemperatureRing.Style = (Style)Application.Current.Resources["PhoneTextTitle3Style"]; txtTemperatureRing.FontFamily = new FontFamily("Segoe WP Light"); txtTemperatureRing.FontSize = 40; txtTemperatureRing.Foreground = new SolidColorBrush(Colors.White); txtTemperatureRing.Text = "°"; txtTemperatureRing.TextAlignment = TextAlignment.Right; txtTemperature.FontFamily = new FontFamily("Segoe WP Light"); txtTemperature.FontSize = 60; txtTemperature.Margin = new Thickness { Left = 0, Bottom = 0, Right = 0, Top = -75 }; txtTemperature.Height = 80; txtTemperature.Width = 145; txtTemperatureRing.Margin = new Thickness { Left = 128, Bottom = 0, Right = 0, Top = -97 }; txtTemperatureRing.Height = 50; txtTemperatureRing.Width = 39; sp.Children.Add(img3); sp.Children.Add(txtTemperature); sp.Children.Add(txtTemperatureRing); //call measure, arrange and updatelayout to prepare for rendering sp.Measure(new Size(173, 173)); sp.Arrange(new Rect(0, 0, 173, 173)); sp.UpdateLayout(); grid.Children.Add(sp); WriteableBitmap wbmp = new WriteableBitmap(173, 173); wbmp.Render(grid, null); wbmp.Invalidate(); //write image to isolated storage string sIsoStorePath = @"\Shared\ShellContent\tile.png"; using (IsolatedStorageFile appStorage = IsolatedStorageFile.GetUserStoreForApplication()) { //ensure directory exists String sDirectory = System.IO.Path.GetDirectoryName(sIsoStorePath); if (!appStorage.DirectoryExists(sDirectory)) { appStorage.CreateDirectory(sDirectory); } using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(sIsoStorePath, System.IO.FileMode.Create, appStorage)) { wbmp.SaveJpeg(stream, 173, 173, 0, 100); } } /// If application uses both PeriodicTask and ResourceIntensiveTask //ShellTile TileToFind = ShellTile.ActiveTiles.FirstOrDefault(x =&gt; x.NavigationUri.ToString().Contains("TileID=2")); ShellTile TileToFind = ShellTile.ActiveTiles.First(); //test if Tile was created if (TileToFind != null) { StandardTileData NewTileData = new StandardTileData { BackgroundImage = new Uri("isostore:Shared/ShellContent/tile.png", UriKind.Absolute), Title = strTitle, Count = null, BackTitle = (string)settings["SelectedCityName"], BackBackgroundImage = new Uri(strBackBackground, UriKind.Relative), BackContent = strWind + Environment.NewLine + strPrecipitation }; //ShellTile.Create(new Uri("/MainPage.xaml?TileID=2", UriKind.Relative), NewTileData); TileToFind.Update(NewTileData); } } if (runNotifyComplete == true) { runNotifyComplete = false; NotifyComplete(); } }//If matches.count } catch { if (runNotifyComplete == true) { runNotifyComplete = false; NotifyComplete(); } } finally { if (runNotifyComplete == true) { runNotifyComplete = false; NotifyComplete(); } } })); } if (runNotifyComplete == true) { runNotifyComplete = false; NotifyComplete(); } } catch { // error handling. if (runNotifyComplete == true) { runNotifyComplete = false; NotifyComplete(); } } finally { } }, request); } catch { if (runNotifyComplete == true) { runNotifyComplete = false; NotifyComplete(); } } finally { } </code></pre> <p>EDIT 1: Here is the code for initializing the background task MessageBox.Show(MyResources.LiveTileToggleMsgBoxText, "Live tile", MessageBoxButton.OK);</p> <pre><code> //start background agent PeriodicTask periodicTask = new PeriodicTask("AppPeriodicAgent0440"); periodicTask.Description = "Periodic task for APP that updates the LiveTile ."; periodicTask.ExpirationTime = System.DateTime.Now.AddDays(14); // If the agent is already registered with the system, if (ScheduledActionService.Find(periodicTask.Name) != null) { ScheduledActionService.Remove("AppPeriodicAgent0440"); } try { //only can be called when application is running in foreground ScheduledActionService.Add(periodicTask); } catch { } </code></pre> <p>EDIT 2: This code is run everytime I exit the application in order to keep the background task alive. I put it in OnNavigatedFrom to avoid slowing down the start up of the app</p> <pre><code>//start background agent PeriodicTask periodicTask = new PeriodicTask("AppPeriodicAgent0440"); periodicTask.Description = "Periodic task for APP that updates the LiveTile."; periodicTask.ExpirationTime = System.DateTime.Now.AddDays(14); // If the agent is already registered with the system, if (ScheduledActionService.Find(periodicTask.Name) != null) { ScheduledActionService.Remove("AppPeriodicAgent0440"); } //only can be called when application is running in foreground ScheduledActionService.Add(periodicTask); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload