Skip to content

Commit

Permalink
Version 1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
NureSyvukhaYaroslav committed Aug 20, 2022
1 parent 6c64fc7 commit 5bcb65f
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 41 deletions.
22 changes: 10 additions & 12 deletions UI/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,27 @@
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="600">
<Grid RenderTransformOrigin="0.5,0.5" HorizontalAlignment="Center" Width="600">
<ListView Margin="275,10,24,121" x:Name="CurrentTasks">
<ListView Margin="357,10,24,65" x:Name="CurrentTasks" SelectionChanged="CurrentTasks_SelectionChanged">
<ListView.View>
<GridView>
<GridViewColumn Header="Name" Width="120" DisplayMemberBinding="{Binding ProcessName}" />
<GridViewColumn Header="Id" Width="60" DisplayMemberBinding="{Binding Id}" />
<GridViewColumn Header="Memory" Width="80" DisplayMemberBinding="{Binding PrivateMemorySize64}" />
<GridViewColumn Header="Name" Width="120" DisplayMemberBinding="{Binding Name}" />
<GridViewColumn Header="Memory" Width="70" DisplayMemberBinding="{Binding MemoryStr}" />
</GridView>
</ListView.View>
</ListView>

<Button x:Name="Update" Content="Update List" Margin="465,0,0,10" Click="Button_Click" HorizontalAlignment="Left" Width="111" Height="50" VerticalAlignment="Bottom"/>
<TextBox x:Name="Input" TextWrapping="Wrap" Width="185" HorizontalAlignment="Left" Margin="275,0,0,10" FontSize="16" TextChanged="Input_TextChanged" Height="50" VerticalAlignment="Bottom" TextAlignment="Center"/>
<Button x:Name="Update" Content="Update List" Margin="497,0,0,10" Click="Button_Click" HorizontalAlignment="Left" Width="79" Height="50" VerticalAlignment="Bottom"/>
<TextBox x:Name="Input" TextWrapping="Wrap" Width="135" HorizontalAlignment="Left" Margin="357,0,0,10" FontSize="16" TextChanged="Input_TextChanged" Height="50" VerticalAlignment="Bottom" TextAlignment="Center"/>

<ListView x:Name="WatchTasks" Margin="10,10,334,10">
<ListView x:Name="WatchTasks" Margin="10,10,248,10" SelectionChanged="WatchTasks_SelectionChanged">
<ListView.View>
<GridView>
<GridViewColumn Header="Name" Width="120" DisplayMemberBinding="{Binding Name}" />
<GridViewColumn Header="Time" Width="100" DisplayMemberBinding="{Binding TimeStr}" />
<GridViewColumn Header="Name" Width="100" DisplayMemberBinding="{Binding Name}" />
<GridViewColumn Header="Full Time" Width="90" DisplayMemberBinding="{Binding FullTimeStr}" />
<GridViewColumn Header="Current Time" Width="80" DisplayMemberBinding="{Binding CurrentTimeStr}" />
<GridViewColumn Header="State" Width="60" DisplayMemberBinding="{Binding StateStr}" />
</GridView>
</ListView.View>
</ListView>
<TextBox x:Name="NameInput" HorizontalAlignment="Left" Margin="275,0,0,66" TextWrapping="Wrap" Width="185" Height="50" VerticalAlignment="Bottom" FontSize="16" TextAlignment="Center"/>
<Button Content="+" HorizontalAlignment="Left" Margin="465,0,0,66" VerticalAlignment="Bottom" Height="50" Width="52" Click="Button_Click_1"/>
<Button Content="-" HorizontalAlignment="Left" Margin="524,0,0,66" VerticalAlignment="Bottom" Height="50" Width="52" Click="Button_Click_2"/>
</Grid>
</Window>
54 changes: 33 additions & 21 deletions UI/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ public MainWindow()

taskService = new TaskService();

CurrentTasks.ItemsSource = Process.GetProcesses();
WatchTasks.ItemsSource = taskService.GetTasks().ToArray();
CurrentTasks.ItemsSource = taskService.MapProcesses(Process.GetProcesses());
WatchTasks.ItemsSource = taskService.GetTasks();
}

protected override void OnClosing(CancelEventArgs e)
Expand All @@ -32,41 +32,53 @@ protected override void OnClosing(CancelEventArgs e)
}
}

private void UpdateList()
private void UpdateList(bool check)
{
CurrentTasks.ItemsSource = Process.GetProcesses()
.Where(x => x.ProcessName.ToLower().Contains(Input.Text.ToLower()));

WatchTasks.ItemsSource = taskService.GetTasks()
.Where(x => x.Name.ToLower().Contains(Input.Text.ToLower())).ToArray();
}

private void Button_Click(object sender, RoutedEventArgs e)
{
if (Input.Text != "")
if (check && Input.Text != "")
{
UpdateList();
CurrentTasks.ItemsSource = taskService.MapProcesses(Process.GetProcesses())
.Where(x => x.Name.ToLower().Contains(Input.Text.ToLower()));

WatchTasks.ItemsSource = taskService.GetTasks()
.Where(x => x.Name.ToLower().Contains(Input.Text.ToLower())).ToArray();
}

else
{
CurrentTasks.ItemsSource = Process.GetProcesses();
WatchTasks.ItemsSource = taskService.GetTasks().ToArray();
CurrentTasks.ItemsSource = taskService.MapProcesses(Process.GetProcesses());

WatchTasks.ItemsSource = taskService.GetTasks();
}

taskService.CheckState();
}

private void Button_Click(object sender, RoutedEventArgs e)
{
UpdateList(true);
}

private void Input_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
{
UpdateList();
UpdateList(true);
}

private void Button_Click_1(object sender, RoutedEventArgs e)
private void WatchTasks_SelectionChanged(object sender, dynamic e)
{
taskService.Add(NameInput.Text);
if (e.AddedItems.Length > 0)
{
taskService.Delete(e.AddedItems[0].Name);
UpdateList(false);
}
}

private void Button_Click_2(object sender, RoutedEventArgs e)
private void CurrentTasks_SelectionChanged(object sender, dynamic e)
{
taskService.Delete(NameInput.Text);
if (e.AddedItems.Length > 0)
{
taskService.Add(e.AddedItems[0].Name);
UpdateList(false);
Input.Text = "";
}
}
}
34 changes: 34 additions & 0 deletions UI/Proc.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;

namespace UI;

public class ProcessDTO
{
public string Name { get; set; }
public long Memory { get; set; }
public string MemoryStr
{
get
{
string[] suffixes = { " B", " KB", " MB", " GB", " TB", " PB" };

for (int i = 0; i < suffixes.Length; i++)
{
long temp = Memory / (int)Math.Pow(1024, i + 1);

if (temp == 0)
{
return (Memory / (int)Math.Pow(1024, i)) + suffixes[i];
}
}

return Memory.ToString();
}
}

public ProcessDTO(string name, long memory)
{
Name = name;
Memory = memory;
}
}
14 changes: 9 additions & 5 deletions UI/Task.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ namespace UI;
public class Task
{
public string Name { get; set; }
public int Time { get; set; }
public string TimeStr => $"{Time / 60}h {Time % 60}m";

public int FullTime { get; set; }
public string FullTimeStr => $"{FullTime / 60}h {FullTime % 60}m";
public int CurrentTime { get; set; }
public string CurrentTimeStr => $"{CurrentTime / 60}h {CurrentTime % 60}m";

public bool State { get; set; }
public string StateStr => (State) ? "Running" : "Stopped";

public Task(string name)
{
Name = name;
}

public override string ToString() => $"{Name} - {Time} seconds";
}
}
53 changes: 50 additions & 3 deletions UI/TaskService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,28 @@ public TaskService()
});
}

public List<Task> GetTasks()
public Task[] GetTasks()
{
return tasks;
return tasks.ToArray().OrderByDescending(x => x.FullTime).ToArray();
}

public ProcessDTO[] MapProcesses(Process[] p)
{
ProcessDTO[] res = new ProcessDTO[p.Length];

for (int i = 0; i < p.Length; i++)
{
var nP = new ProcessDTO(p[i].ProcessName, p[i].PrivateMemorySize64);
res[i] = nP;
}

return res.OrderByDescending(x => x.Memory).ToArray();
}

public void Start()
{
tasks.ForEach(x => x.CurrentTime = 0);

while (true)
{
var wait = System.Threading.Tasks.Task.Delay(60000);
Expand All @@ -62,8 +77,13 @@ public void Start()

if (process != null)
{
task.Time++;
task.FullTime++;
task.CurrentTime++;
task.State = true;
continue;
}

task.State = false;
}

using (var sw = new StreamWriter(path))
Expand All @@ -75,13 +95,40 @@ public void Start()
}
}

public void CheckState()
{
for (int i = 0; i < tasks.Count; i++)
{
var task = tasks[i];
var process = Process.GetProcessesByName(task.Name).FirstOrDefault();

if (process != null)
{
task.State = true;
continue;
}

task.State = false;
}

using (var sw = new StreamWriter(path))
{
sw.WriteLine(JsonConvert.SerializeObject(tasks));
}
}

public void Add(string name)
{
if (tasks.Where(x => x.Name == name).Any())
{
return;
}

if (Process.GetProcessesByName(name).FirstOrDefault() == null)
{
return;
}

tasks.Add(new Task(name));

using (var sw = new StreamWriter(path))
Expand Down

0 comments on commit 5bcb65f

Please sign in to comment.