public async Task <bool> UpdateAsync(StatusInfo info, CancellationToken token) // return remain time information { int len = 100; info.StatusMax = len; var tempData = await Task.Run(() => GetData(info, token)); if (token.IsCancellationRequested) { return(false); } info.CurrentStatus = len; var comparer = new ProcessComparer(); //Delete old elements var old = ProcessInfos.Except(tempData, comparer).ToList(); foreach (var d in old) { ProcessInfos.Remove(d); } //Add new elements var newElements = tempData.Except(ProcessInfos, comparer); foreach (var element in newElements) { ProcessInfos.Add(element); } return(true); }
/// <summary>Constructor.</summary> public ProcessSelectionDialog() { InitializeComponent(); // Hide the caret on the filter text box (by setting it to a transparent brush) m_defaultFilterTextBoxBrush = m_txtFilter.CaretBrush; m_txtFilter.CaretBrush = TRANSPARENT_BRUSH; // Use a custom sorting algorithm for the list of processes view ProcessComparer processComparer = new ProcessComparer(); ListCollectionView processesListCollectionView = (ListCollectionView)m_lstProcesses.ItemsSource; processesListCollectionView.CustomSort = processComparer; // Configure the Timer used to update the list of processes Action updateProcessesListAction = () => { Process selectedProcess = (Process)m_lstProcesses.SelectedItem; AvailableProcesses.Clear(); foreach (Process curProc in Process.GetProcesses()) { AvailableProcesses.Add(curProc); if (selectedProcess != null && curProc.Id == selectedProcess.Id) { m_lstProcesses.SelectedItem = curProc; } } }; m_processesListUpdateTimer.Elapsed += (sender, args) => { // Use the dispatcher to update the list of processes in the same Thread that // created the dialog this.Dispatcher.Invoke(updateProcessesListAction); }; // Manually fire the method that updates the list of processes, so that the list gets updated immediatelly // when the dialog is opened updateProcessesListAction(); m_processesListUpdateTimer.Start(); }