title: ScheduledTaskAgent not working for updating tiles in Windows Phone
body: <p>I am working on a Windows Phone 7 app and trying to implement the features of a Windows Phone 8 like wide tiles into it. I achieved it using reflection but when i wanted to update the tile using a ScheduledAgent for a Periodic Task the tile is not getting created.</p>
  
  <p>The scheduled agent OnInvoke code looks like </p>
  
  <pre><code>protected override void OnInvoke(ScheduledTask task)
  {
      //TODO: Add code to perform your task in background
       if (task is PeriodicTask)
       {
           //Update the tile using Scheduled Task
           CreateTileForWindowsPhone.CreateWideTile();
       }
       NotifyComplete();
  } 
  </code></pre>
  
  <p>I created the tile using this code</p>
  
  <pre><code>public class CreateTileForWindowsPhone
  {
      private static Version TargetedVersion = new Version(7, 10, 8858);
      public static bool IsTargetedVersion { get { return Environment.OSVersion.Version &gt;= TargetedVersion; } }
  
      public static void CreateWideTile()
      {
          if (IsTargetedVersion)
          {
              try
              {
                  // Get the new FlipTileData type.
                  Type flipTileDataType = Type.GetType("Microsoft.Phone.Shell.FlipTileData, Microsoft.Phone");
                  // Get the ShellTile type so we can call the new version of "Update" that takes the new Tile templates.
  
                  Type shellTileType = Type.GetType("Microsoft.Phone.Shell.ShellTile, Microsoft.Phone");
                  // Loop through any existing Tiles that are pinned to Start.
  
                  QuotesCollection aq = new QuotesCollection();
                  Random rand = new Random();
                  int randNum = rand.Next(0, 163);
  
                  //String wideBackStr = "Dont be the same, Be Better.";
                  String wideBackStr = aq.quotes[randNum];
  
                  foreach (var tileToUpdate in ShellTile.ActiveTiles)
                  {
                      // Get the constructor for the new FlipTileData class and assign it to our variable to hold the Tile properties.
                      var UpdateTileData = flipTileDataType.GetConstructor(new Type[] { }).Invoke(null);
  
                      // Set the properties. 
                      SetProperty(UpdateTileData, "WideBackgroundImage", new Uri("/images/QuottedWideTile.png", UriKind.Relative));
                      SetProperty(UpdateTileData, "WideBackContent", wideBackStr);
                      // Invoke the new version of ShellTile.Update.
                      shellTileType.GetMethod("Update").Invoke(tileToUpdate, new Object[] { UpdateTileData });
                      break;
                  }
              }
              catch
              {
                  MessageBox.Show("Tile Error Caught");
              }
          }
      }
  
      private static void SetProperty(object instance, string name, object value)
      {
          var setMethod = instance.GetType().GetProperty(name).GetSetMethod();
          setMethod.Invoke(instance, new object[] { value });
      }
  }
  </code></pre>
  
  <p>i made a register agent method to register the Periodic Task</p>
  
  <pre><code>    private void RegisterAgent()
      {
          string taskName = "MyTask";
          try
          {
  
              if (ScheduledActionService.Find(taskName) != null)
              {
                  //if the agent exists, remove and then add it to ensure
                  //the agent's schedule is updated to avoid expiration
                  ScheduledActionService.Remove(taskName);
              }
  
              PeriodicTask periodicTask = new PeriodicTask(taskName);
              periodicTask.Description = "Random Quote Update On Tile";
              ScheduledActionService.Add(periodicTask);
  
          }
          catch (InvalidOperationException exception)
          {
              MessageBox.Show(exception.Message);
          }
          catch (SchedulerServiceException schedulerException)
          {
              MessageBox.Show(schedulerException.Message);
          }
      }
  </code></pre>
  
  <p>and called the register in the app launching</p>
  
  <pre><code>    private void Application_Launching(object sender, LaunchingEventArgs e)
      {
          RegisterAgent();
      }
  </code></pre>
  
  <p>But the wide tile is not being created when i run the app.
  Wide tile creation used to work before i used scheduled agents to update the tile. I used to create a wide tile just by calling that function in application launching.</p>
  
  <pre><code>    private void Application_Launching(object sender, LaunchingEventArgs e)
      {
          CreateTileForWindowsPhone.CreateWideTile();
      }
  </code></pre>
  
  <p>WHy is the tile not being created. Am i doing something wrong?</p>