Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions TCSetup/TCSetup.vdproj
Original file line number Diff line number Diff line change
Expand Up @@ -209,24 +209,24 @@
{
"Name" = "8:Microsoft Visual Studio"
"ProductName" = "8:TimeControl"
"ProductCode" = "8:{58790286-6BF3-4D15-BE41-3514CFB9D21B}"
"PackageCode" = "8:{15145DC8-E457-4268-AA72-57CBF07212D1}"
"ProductCode" = "8:{363F9324-4EDC-45ED-8286-322CA5109544}"
"PackageCode" = "8:{07EBB234-3576-4FAD-8A56-24A64982FEB6}"
"UpgradeCode" = "8:{A7DC5BC4-7E3E-4B58-A0AD-3C9AC9F873C1}"
"AspNetVersion" = "8:4.0.30319.0"
"RestartWWWService" = "11:FALSE"
"RemovePreviousVersions" = "11:TRUE"
"DetectNewerInstalledVersion" = "11:TRUE"
"InstallAllUsers" = "11:FALSE"
"ProductVersion" = "8:3.1.3"
"ProductVersion" = "8:3.2.0"
"Manufacturer" = "8:SamHou"
"ARPHELPTELEPHONE" = "8:"
"ARPHELPLINK" = "8:"
"ARPHELPLINK" = "8:https://github.com/SamHou2007/ComputerTimeControl"
"Title" = "8:TCSetup"
"Subject" = "8:"
"ARPCONTACT" = "8:SamHou"
"Keywords" = "8:"
"ARPCOMMENTS" = "8:TimeControl安装程序"
"ARPURLINFOABOUT" = "8:"
"ARPURLINFOABOUT" = "8:https://github.com/SamHou2007"
"ARPPRODUCTICON" = "8:_9BF8F60AD5F2474D82994CD2D95F44A1"
"ARPIconIndex" = "3:0"
"SearchPath" = "8:"
Expand Down Expand Up @@ -823,7 +823,7 @@
{
"{5259A561-127C-4D43-A0A1-72F10C7B3BF8}:_5003B007B32D429B83CC5372AB9FC0AF"
{
"SourcePath" = "8:..\\TimeControlConsole\\obj\\Debug\\net6.0-windows10.0.22000.0\\apphost.exe"
"SourcePath" = "8:..\\TimeControlConsole\\obj\\Release\\net6.0-windows10.0.22000.0\\apphost.exe"
"TargetName" = "8:"
"Tag" = "8:"
"Folder" = "8:_57FF55C0487B4507A7ADC64F8E5966A5"
Expand Down Expand Up @@ -851,7 +851,7 @@
}
"{5259A561-127C-4D43-A0A1-72F10C7B3BF8}:_B7C31D1C4E42443D89CA87F9596A43B7"
{
"SourcePath" = "8:..\\TimeControl\\obj\\Debug\\net6.0-windows10.0.22000.0\\apphost.exe"
"SourcePath" = "8:..\\TimeControl\\obj\\Release\\net6.0-windows10.0.22000.0\\apphost.exe"
"TargetName" = "8:"
"Tag" = "8:"
"Folder" = "8:_57FF55C0487B4507A7ADC64F8E5966A5"
Expand Down
21 changes: 13 additions & 8 deletions TimeControl/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,39 @@

namespace TimeControl
{
[Serializable]
public class App
{
private readonly string name;
public string Name { get { return name; }}

public string Name
{ get { return name; } }

internal int time;

/// <summary>
/// 返回进程的简要概述
/// </summary>
/// <returns>进程的简要概述</returns>
public override string ToString()
{
return Name + " 已使用 " + time+" 秒!";
return Name + " 已使用 " + time + " 秒!";
}
public App(string name,int time)

public App(string name, int time)
{
this.time = time;
this.name = name;
}

/// <summary>
/// 运行一次(一秒)
/// </summary>
public virtual void Run(StreamWriter streamWriter)
public virtual void Run()
{
time++;
streamWriter.WriteLine(Name);
streamWriter.WriteLine(time);
streamWriter.WriteLine("//");
}

/// <summary>
/// 重设时间
/// </summary>
Expand All @@ -43,4 +48,4 @@ public void Reset()
time = 0;
}
}
}
}
84 changes: 26 additions & 58 deletions TimeControl/AppController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,63 +9,21 @@

namespace TimeControl
{
[Serializable]
public class AppController
{
private FileStream fileStream = new(TimeControlFile.TimeFileLocation,
FileMode.OpenOrCreate,
FileAccess.ReadWrite, FileShare.None);
private StreamWriter streamWriter;
private ListBox listBox;
private List<App> apps;
private Timer timer;

public AppController(ListBox listBox, Timer timer)
{
streamWriter = new(fileStream);
this.listBox = listBox;
apps = new List<App>();
this.timer = timer;
StreamReader streamReader = new(fileStream);
int lineNumber = 1;
string name = null;
int time = 0;
int timeLimit = 0;
while (!streamReader.EndOfStream)//读取文件,添加进程
{
string line = streamReader.ReadLine();
if (line == "//")
{
if (timeLimit == 0)
apps.Add(new App(name, time));
else
apps.Add(new LimitedApp(name, time, timeLimit));

lineNumber = 1;
name = null;
time = 0;
timeLimit = 0;

continue;
}
else
{
if (lineNumber == 1)
name = line;
else if (lineNumber == 2)
time = Convert.ToInt32(line);
else if (lineNumber == 3)
timeLimit = Convert.ToInt32(line);

lineNumber++;
}
}
Refresh();
Refresh(timer, listBox);
}

/// <summary>
/// 刷新列表显示
/// </summary>
public void Refresh()
public void Refresh(Timer timer, ListBox listBox)
{
timer.Stop();
listBox.Items.Clear();
Expand All @@ -75,11 +33,12 @@ public void Refresh()
}
timer.Start();
}

/// <summary>
/// 根据名称添加进程
/// </summary>
/// <param name="name">要添加的进程名称</param>
public void AddByName(string name)
public void AddByName(string name, Timer timer, ListBox listBox)
{
timer.Stop();
Process[] processes = Process.GetProcessesByName(name);
Expand All @@ -94,14 +53,15 @@ public void AddByName(string name)
{
MessageBox.Show("错误", ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
this.Refresh();
Refresh(timer, listBox);
}

/// <summary>
/// 根据名称添加时间受限的进程
/// </summary>
/// <param name="name">进程名称</param>
/// <param name="limitTime">限制时长(秒)</param>
public void AddByName(string name, int limitTime)
public void AddByName(string name, int limitTime, Timer timer, ListBox listBox)
{
timer.Stop();
Process[] processes = Process.GetProcessesByName(name);
Expand All @@ -116,47 +76,55 @@ public void AddByName(string name, int limitTime)
{
MessageBox.Show("错误", ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
this.Refresh();
Refresh(timer, listBox);
}

/// <summary>
/// 跟踪所有进程,增加一秒
/// </summary>
public void Run()
{
fileStream.SetLength(0);
foreach (App app in apps)//计算进程时间
{
if (Process.GetProcessesByName(app.Name).Length != 0)
{
if (app is LimitedApp)
{
LimitedApp limitedApp = app as LimitedApp;
limitedApp.Run(streamWriter);
limitedApp.Run();
}
else
app.Run(streamWriter);
app.Run();
}
}
streamWriter.Flush();
}

/// <summary>
/// 移除所列表所选的进程
/// </summary>
public void Remove()
public void Remove(Timer timer, ListBox listBox)
{
timer.Stop();
if (listBox.SelectedIndex >= 0)
apps.RemoveAt(listBox.SelectedIndex);
Refresh();
Refresh(timer, listBox);
}

/// <summary>
/// 删除所有监控
/// </summary>
public void RemoveAll()
public void RemoveAll(Timer timer, ListBox listBox)
{
timer.Stop();
apps.Clear();
Refresh();
Refresh(timer, listBox);
}

public void ResetAll(Timer timer, ListBox listBox)
{
foreach (App app in apps)
app.Reset();
Refresh(timer, listBox);
}
}
}
}
41 changes: 27 additions & 14 deletions TimeControl/ControlPanel.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading