Reputation: 31
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Diagnostics;
using System.IO;
namespace CustomizedWPFTaskManager
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
//Load Function
OnLoad();
}
private void OnLoad()
{
System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
dispatcherTimer.Tick += dispatcherTimer_Tick;
dispatcherTimer.Interval = new TimeSpan(0, 0, 2);
dispatcherTimer.Start();
}
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
//- Refresh process list
Process pList = new Process();
List<String> procList = new List<string>(new string[] { "" });
foreach (Process p in Process.GetProcesses())
{
try
{
FileInfo fi = new FileInfo(p.MainModule.FileName);
procList.Add(fi.Name);
TaskViewBox1.Items.Add(fi.Name);
}
catch { }
}
countLabel.Content = TaskViewBox1.Items.Count + " PROCESSES RUNNING";
}
}
}
This is what I have so far, but my problem is processes will keep adding even though they are already their (duplicates). Also I am new to this, my boss has been teaching me to help out around the shop. He wanted me to learn WPF as well. This was what I thought a good first project, any help on how to remove duplicates? Also if its not to much can you explain what you did a little but so I am not just copying the code but actually learning it for next time.
Upvotes: 3
Views: 1280
Reputation: 128061
Let's take a look at a very basic example that makes use of the MVVM achitectural pattern.
First the view model. It has a Processes
property, which is an ObservableCollection<Process>
and a Tick
event handler of a DispatcherTimer
, which updates the collection.
public class ViewModel
{
public ObservableCollection<Process> Processes { get; }
= new ObservableCollection<Process>();
public ViewModel()
{
var timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(2) };
timer.Tick += UpdateProcesses;
timer.Start();
}
private void UpdateProcesses(object sender, EventArgs e)
{
var currentIds = Processes.Select(p => p.Id).ToList();
foreach (var p in Process.GetProcesses())
{
if (!currentIds.Remove(p.Id)) // it's a new process id
{
Processes.Add(p);
}
}
foreach (var id in currentIds) // these do not exist any more
{
Processes.Remove(Processes.First(p => p.Id == id));
}
}
}
Now the view. It is a simple ListBox that shows the Id
and ProcessName
of the collection of Processes. Its ItemsSource
is bound to the Processes
property of the view model.
<ListBox ItemsSource="{Binding Processes}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Id}" Width="50"/>
<TextBlock Text="{Binding ProcessName}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Finally, an instance of the view model has to be assigned to the DataContext of the MainWindow. This can be done in XAML
<Window.DataContext>
<local:ViewModel/>
</Window.DataContext>
or in code behind
public MainWindow()
{
InitializeComponent();
DataContext = new ViewModel();
}
Upvotes: 1