private void LoadRecentContexts(int limit) { lstRecent.Items.Clear(); var contexts = _contextService.GetMostRecent(limit); foreach (var context in contexts) { var lvi = new ListViewItem(); lvi.Text = context.Id.ToString(); lvi.SubItems.Add(context.ContextName); lvi.SubItems.Add(context.CreationDateTime.ToString()); lstRecent.Items.Add(lvi); } }
/// <summary> /// Populate the context lists. /// </summary> /// <returns>False if update has been skipped due to the user selection</returns> private bool UpdateContextLists() { // get new results var recentContexts = _contextService.GetMostRecent((int)_settings.MostRecentLimit); var closerContexts = _serviceClient.GetCloserContexts(); List <ContextPreferenceItem> closerContextItems = new List <ContextPreferenceItem>(); foreach (var closerContext in closerContexts.OrderByDescending(c => c.Value)) { var context = _contextService.GetById(closerContext.ContextId); if (context == null) { continue; } var item = new ContextPreferenceItem { Name = context.ContextName, Value = closerContext.Value.ToString(), ContextId = closerContext.ContextId }; closerContextItems.Add(item); } try { // update could take time // check again for user selection bool closerSelected = listCloser.Dispatcher.Invoke( new Func <bool>( () => { return(listCloser.SelectedItem != null); }) ); bool recentSelected = listRecent.Dispatcher.Invoke( new Func <bool>( () => { return(listRecent.SelectedItem != null); }) ); if (!recentSelected && !closerSelected) { // update UI listRecent.Dispatcher.Invoke(new System.Action( () => { listRecent.ItemsSource = recentContexts; })); listCloser.Dispatcher.Invoke(new System.Action( () => { listCloser.ItemsSource = closerContextItems; })); // update performed return(true); } } catch { // tried to access UI after Dispose() return(false); } // update skipped return(false); }