title: Updating UI Thread from TimerCallback Function Windows Phone 8
body: <p>a little stumped here.</p>
  
  <p>I have a TimerCallback that fires every 10 seconds which contains Geopoints to put on a map. I try to add these to the map from within the timer callback function, however since it is in a different thread, I am unable to do this. I receive the following errors:</p>
  
  <blockquote>
    <p>An exception of type 'System.IO.FileNotFoundException' occurred in
    mscorlib.ni.dll and wasn't handled before a managed/native boundary.</p>
    
    <p>A first chance exception of type 'System.UnauthorizedAccessException'
    occurred in System.Windows.ni.dll</p>
  </blockquote>
  
  <p>How would can I circumvent this? I thought that maybe added a NotifyCollectionChanged listener would work, however I am still having the same problem. Code is shown below.</p>
  
  <pre><code>    private ObservableCollection&lt;Bus&gt; _busList;
      private Timer _timer = null;
      public ItemViewModel route;
  
      public ObservableCollection&lt;Bus&gt; BusList
      {
          get { return _busList; }
          set { _busList = value; }
      }
      //public LocationManager locMan = LocationManager.Instance;
      // Constructor
      public DetailsPage()
      {
          InitializeComponent();
  
          // Sample code to localize the ApplicationBar
          //BuildLocalizedApplicationBar();
      }
  
      // When page is navigated to set data context to selected item in list
      protected override void OnNavigatedTo(NavigationEventArgs e)
      {
          route = null;
          if (DataContext == null)
          {
              string selectedIndex = "";
              if (NavigationContext.QueryString.TryGetValue("selectedItem", out selectedIndex))
              {
                  int index = int.Parse(selectedIndex);
                  DataContext = App.ViewModel.Items[index];
                  route = App.ViewModel.Items[index];
                  if (_timer == null)
                  {
                      TimerCallback tcb = obtainJSON;
                      _timer = new Timer(tcb, route.RouteID, 0, 10000);
                  }
                  else
                  {
                      _timer.Change(0, 10000);
                  }
                  if (BusList == null)
                  {
                      BusList = new ObservableCollection&lt;Bus&gt;();
                  }
                  else
                  {
                      BusList.Clear();
                  }
                  BusList.CollectionChanged += HandleBusAdded;
              }
          }
      }
  
      private void HandleBusAdded(object sender, NotifyCollectionChangedEventArgs e)
      {
          if (e.Action == NotifyCollectionChangedAction.Reset)
          {
              Debug.WriteLine("Everything was cleared");
          }
          else
          {
              foreach (Bus item in e.NewItems)
              {
                  Debug.WriteLine(item.vehicleID);
                  Polygon polygon = new Polygon();
                  polygon.Points.Add(new Point(0, 0));
                  polygon.Points.Add(new Point(0, 75));
                  polygon.Points.Add(new Point(25, 0));
                  polygon.Fill = new SolidColorBrush(Colors.Blue);
  
                  // Create a MapOverlay and add marker
                  MapOverlay overlay = new MapOverlay();
                  overlay.Content = polygon;
                  overlay.GeoCoordinate = new GeoCoordinate(item.lat, item.lng);
                  overlay.PositionOrigin = new Point(0.0, 1.0);
                  MapLayer mapLayer = new MapLayer();
                  mapLayer.Add(overlay);
                  //mapView.Layers.Add(mapLayer);
  
                  this.Dispatcher.BeginInvoke(() =&gt;
                  {
                      mapView.Layers.Add(mapLayer);
                  });
              }
          }
  
      }
  
      public void obtainJSON(Object stateInfo)
      {
  
          string url = "http://xxx.xxx.xxx" + stateInfo.ToString();
          var client = new WebClient();
          client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(Decrypt);
          client.DownloadStringAsync(new Uri(url));
      }
  
      public void Decrypt(Object sender, DownloadStringCompletedEventArgs e)
      {
          if (BusList.Count &gt; 0)
          {
              BusList.Clear();
          }
          if (!e.Cancelled &amp;&amp; e.Error == null)
          {
              var temp = new List&lt;string&gt;();
              string[] buses = e.Result.Split('\n');
              foreach (string bus in buses)
              {
                  if (!string.IsNullOrWhiteSpace(bus) &amp;&amp; !string.IsNullOrEmpty(bus))
                  {
                      temp.Add(bus);
                  }
              }
              foreach (string item in temp)
              {
                  string[] busInfo = item.Split(',');
                  Bus newBus = new Bus(busInfo[0], busInfo[1], busInfo[2]);
                  BusList.Add(newBus);
  
              }
  
              // This is where I initially tried
              /*Polygon polygon = new Polygon();
              polygon.Points.Add(new Point(0, 0));
              polygon.Points.Add(new Point(0, 75));
              polygon.Points.Add(new Point(25, 0));
              polygon.Fill = new SolidColorBrush(Colors.Blue);
  
              // Enable marker to be tapped for location information
              // polygon.Tag = new GeoCoordinate(BusList[0].lat, BusList[0].lng);
  
              // Create a MapOverlay and add marker
              MapOverlay overlay = new MapOverlay();
              overlay.Content = polygon;
              overlay.GeoCoordinate = new GeoCoordinate(BusList[0].lat, BusList[0].lng);
              overlay.PositionOrigin = new Point(0.0, 1.0);
              MapLayer mapLayer = new MapLayer();
              mapLayer.Add(overlay);
  
              this.Dispatcher.BeginInvoke(() =&gt;
              {
                  mapView.Layers.Add(mapLayer);
              });*/
              Debug.WriteLine("Present buses " + BusList[0].vehicleID + ", " + BusList[1].vehicleID);
          }
      }
  }
  </code></pre>