Skip to content

Commit 1c2de0f

Browse files
committed
增加进程限时功能
1 parent 68ddac3 commit 1c2de0f

11 files changed

+461
-11
lines changed

TimeControl/App.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66

77
namespace TimeControl
88
{
9-
internal class App
9+
public class App
1010
{
1111
private string name;
1212
private string location;
1313
public string Name { get { return name; }}
14-
private int time;
14+
internal int time;
1515

1616
public string ReportApp()
1717
{
@@ -21,11 +21,15 @@ public App(string name, string location)
2121
{
2222
this.name = name;
2323
this.location = location;
24-
time = 0;
24+
Reset();
2525
}
26-
public void Run()
26+
public virtual void Run()//运行一次(一秒)
2727
{
2828
time++;
2929
}
30+
public void Reset()//重设时间
31+
{
32+
time = 0;
33+
}
3034
}
3135
}

TimeControl/ControlPanel.Designer.cs

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

TimeControl/ControlPanel.cs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public partial class ControlPanel : Form
1919
private bool hide = false;//指示启动后是否需要隐藏
2020
private bool isClosable = false;//指示当前是否可以关闭
2121
private int unlockPasswordHash = 0;//密码哈希值,用作比对
22-
ListController controller;//列表、计时控制器
22+
private ListController controller;//列表、计时控制器
2323
public ControlPanel(bool hide)
2424
{
2525
InitializeComponent();
@@ -29,7 +29,7 @@ public ControlPanel(bool hide)
2929
unlockPasswordHash = Convert.ToInt32(File.ReadAllText(PasswordFile.tcPassLocation));
3030
PasswordSet();
3131
}
32-
controller=new(usageBox,processMonitorTimer);
32+
controller = new(usageBox, processMonitorTimer);
3333
}
3434

3535
private void StartButton_Click(object sender, EventArgs e)//启动屏保程序
@@ -88,12 +88,26 @@ private void LinkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs
8888

8989
private void AppAddButton_Click(object sender, EventArgs e)//添加打开的窗口
9090
{
91-
controller.AddByName(processNameBox.Text);
91+
if (processNameBox.Text.ToLower() == "timecontrol" ||
92+
processNameBox.Text.ToLower() == "timecontrolconsole")
93+
{
94+
return;
95+
}
96+
TimeInput timeInput = new(controller, processNameBox.Text);
97+
timeInput.ShowDialog();
9298
}
9399

94100
private void RemoveButton_Click(object sender, EventArgs e)//移除所有的已添加窗口
95101
{
96-
controller.Remove();
102+
//检测密码设置
103+
if (unlockPasswordHash != 0)
104+
{
105+
PasswordInput passwordInput = new(unlockPasswordHash);
106+
if (passwordInput.ShowDialog() == DialogResult.OK)
107+
controller.Remove();
108+
}
109+
else
110+
controller.Remove();
97111
}
98112

99113
private void RefreshButton_Click(object sender, EventArgs e)//重新获取所有软件所用时间

TimeControl/LimitWarningWindow.Designer.cs

Lines changed: 74 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

TimeControl/LimitWarningWindow.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.ComponentModel;
4+
using System.Data;
5+
using System.Drawing;
6+
using System.Linq;
7+
using System.Text;
8+
using System.Threading.Tasks;
9+
using System.Windows.Forms;
10+
11+
namespace TimeControl
12+
{
13+
public partial class LimitWarningWindow : Form
14+
{
15+
LimitedApp warningApp;
16+
public LimitWarningWindow(LimitedApp warningApp)
17+
{
18+
InitializeComponent();
19+
displayLabel.Text = $"应用{warningApp.Name}即将达到时间限制,请保存所有工作,然后提前关闭进程";
20+
this.warningApp = warningApp;
21+
}
22+
23+
private void closeButton_Click(object sender, EventArgs e)
24+
{
25+
warningApp.Ban();//禁用掉app
26+
Close();
27+
}
28+
}
29+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<root>
2+
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
3+
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
4+
<xsd:element name="root" msdata:IsDataSet="true">
5+
<xsd:complexType>
6+
<xsd:choice maxOccurs="unbounded">
7+
<xsd:element name="metadata">
8+
<xsd:complexType>
9+
<xsd:sequence>
10+
<xsd:element name="value" type="xsd:string" minOccurs="0" />
11+
</xsd:sequence>
12+
<xsd:attribute name="name" use="required" type="xsd:string" />
13+
<xsd:attribute name="type" type="xsd:string" />
14+
<xsd:attribute name="mimetype" type="xsd:string" />
15+
<xsd:attribute ref="xml:space" />
16+
</xsd:complexType>
17+
</xsd:element>
18+
<xsd:element name="assembly">
19+
<xsd:complexType>
20+
<xsd:attribute name="alias" type="xsd:string" />
21+
<xsd:attribute name="name" type="xsd:string" />
22+
</xsd:complexType>
23+
</xsd:element>
24+
<xsd:element name="data">
25+
<xsd:complexType>
26+
<xsd:sequence>
27+
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
28+
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
29+
</xsd:sequence>
30+
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
31+
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
32+
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
33+
<xsd:attribute ref="xml:space" />
34+
</xsd:complexType>
35+
</xsd:element>
36+
<xsd:element name="resheader">
37+
<xsd:complexType>
38+
<xsd:sequence>
39+
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
40+
</xsd:sequence>
41+
<xsd:attribute name="name" type="xsd:string" use="required" />
42+
</xsd:complexType>
43+
</xsd:element>
44+
</xsd:choice>
45+
</xsd:complexType>
46+
</xsd:element>
47+
</xsd:schema>
48+
<resheader name="resmimetype">
49+
<value>text/microsoft-resx</value>
50+
</resheader>
51+
<resheader name="version">
52+
<value>2.0</value>
53+
</resheader>
54+
<resheader name="reader">
55+
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
56+
</resheader>
57+
<resheader name="writer">
58+
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
59+
</resheader>
60+
<metadata name="backgroundWorker1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
61+
<value>17, 17</value>
62+
</metadata>
63+
</root>

TimeControl/LimitedApp.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using System.Diagnostics;
7+
using System.Windows.Forms;
8+
9+
namespace TimeControl
10+
{
11+
public class LimitedApp : App
12+
{
13+
private int timeLimit;
14+
public LimitedApp(string name, string location,int timeLimit) : base(name, location)
15+
{
16+
this.timeLimit = timeLimit;
17+
}
18+
public override void Run()//运行一次(一秒)
19+
{
20+
base.Run();
21+
if (time == timeLimit - 30)
22+
{
23+
LimitWarningWindow warningWindow = new(this);
24+
warningWindow.Show();
25+
}
26+
if (time>=timeLimit)
27+
{
28+
Ban();
29+
}
30+
}
31+
public void Ban()//禁用掉该程序
32+
{
33+
Process[] processes = Process.GetProcessesByName(Name);
34+
foreach (Process process in processes)
35+
{
36+
process.Kill();
37+
}
38+
time = timeLimit;
39+
}
40+
}
41+
}

TimeControl/ListController.cs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
namespace TimeControl
1010
{
11-
internal class ListController
11+
public class ListController
1212
{
1313
private ListBox listBox;
1414
private List<App> apps;
@@ -49,14 +49,38 @@ public void AddByName(string name)
4949
}
5050
this.Refresh();
5151
}
52+
public void AddByName(string name,int limitTime)
53+
{
54+
timer.Stop();
55+
Process[] processes = Process.GetProcessesByName(name);
56+
try
57+
{
58+
foreach (Process process in processes)
59+
{
60+
apps.Add(new LimitedApp(process.ProcessName, process.MainModule.FileName,limitTime));
61+
}
62+
}
63+
catch (Exception ex)
64+
{
65+
MessageBox.Show("错误", ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Error);
66+
}
67+
this.Refresh();
68+
}
5269
public void Run()
5370
{
5471
foreach (App app in apps)//计算进程时间
5572
{
5673
if (Process.GetProcessesByName(app.Name).Length != 0)
57-
{ app.Run(); }
74+
{
75+
if (app is LimitedApp)
76+
{
77+
LimitedApp limitedApp=app as LimitedApp;
78+
limitedApp.Run();
79+
}
80+
else
81+
app.Run();
82+
}
5883
}
59-
Refresh();
6084
}
6185
public void Remove()
6286
{

0 commit comments

Comments
 (0)