Skip to content

Commit 214999b

Browse files
committed
串行化更新
1 parent c460a21 commit 214999b

File tree

6 files changed

+90
-91
lines changed

6 files changed

+90
-91
lines changed

TimeControl/App.cs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,39 @@
77

88
namespace TimeControl
99
{
10+
[Serializable]
1011
public class App
1112
{
1213
private readonly string name;
13-
public string Name { get { return name; }}
14+
15+
public string Name
16+
{ get { return name; } }
17+
1418
internal int time;
19+
1520
/// <summary>
1621
/// 返回进程的简要概述
1722
/// </summary>
1823
/// <returns>进程的简要概述</returns>
1924
public override string ToString()
2025
{
21-
return Name + " 已使用 " + time+" 秒!";
26+
return Name + " 已使用 " + time + " 秒!";
2227
}
23-
public App(string name,int time)
28+
29+
public App(string name, int time)
2430
{
2531
this.time = time;
2632
this.name = name;
2733
}
34+
2835
/// <summary>
2936
/// 运行一次(一秒)
3037
/// </summary>
31-
public virtual void Run(StreamWriter streamWriter)
38+
public virtual void Run()
3239
{
3340
time++;
34-
streamWriter.WriteLine(Name);
35-
streamWriter.WriteLine(time);
36-
streamWriter.WriteLine("//");
3741
}
42+
3843
/// <summary>
3944
/// 重设时间
4045
/// </summary>
@@ -43,4 +48,4 @@ public void Reset()
4348
time = 0;
4449
}
4550
}
46-
}
51+
}

TimeControl/AppController.cs

Lines changed: 19 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -9,63 +9,21 @@
99

1010
namespace TimeControl
1111
{
12+
[Serializable]
1213
public class AppController
1314
{
14-
private FileStream fileStream = new(TimeControlFile.TimeFileLocation,
15-
FileMode.OpenOrCreate,
16-
FileAccess.ReadWrite, FileShare.None);
17-
private StreamWriter streamWriter;
18-
private ListBox listBox;
1915
private List<App> apps;
20-
private Timer timer;
2116

2217
public AppController(ListBox listBox, Timer timer)
2318
{
24-
streamWriter = new(fileStream);
25-
this.listBox = listBox;
2619
apps = new List<App>();
27-
this.timer = timer;
28-
StreamReader streamReader = new(fileStream);
29-
int lineNumber = 1;
30-
string name = null;
31-
int time = 0;
32-
int timeLimit = 0;
33-
while (!streamReader.EndOfStream)//读取文件,添加进程
34-
{
35-
string line = streamReader.ReadLine();
36-
if (line == "//")
37-
{
38-
if (timeLimit == 0)
39-
apps.Add(new App(name, time));
40-
else
41-
apps.Add(new LimitedApp(name, time, timeLimit));
42-
43-
lineNumber = 1;
44-
name = null;
45-
time = 0;
46-
timeLimit = 0;
47-
48-
continue;
49-
}
50-
else
51-
{
52-
if (lineNumber == 1)
53-
name = line;
54-
else if (lineNumber == 2)
55-
time = Convert.ToInt32(line);
56-
else if (lineNumber == 3)
57-
timeLimit = Convert.ToInt32(line);
58-
59-
lineNumber++;
60-
}
61-
}
62-
Refresh();
20+
Refresh(timer, listBox);
6321
}
6422

6523
/// <summary>
6624
/// 刷新列表显示
6725
/// </summary>
68-
public void Refresh()
26+
public void Refresh(Timer timer, ListBox listBox)
6927
{
7028
timer.Stop();
7129
listBox.Items.Clear();
@@ -75,11 +33,12 @@ public void Refresh()
7533
}
7634
timer.Start();
7735
}
36+
7837
/// <summary>
7938
/// 根据名称添加进程
8039
/// </summary>
8140
/// <param name="name">要添加的进程名称</param>
82-
public void AddByName(string name)
41+
public void AddByName(string name, Timer timer, ListBox listBox)
8342
{
8443
timer.Stop();
8544
Process[] processes = Process.GetProcessesByName(name);
@@ -94,14 +53,15 @@ public void AddByName(string name)
9453
{
9554
MessageBox.Show("错误", ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Error);
9655
}
97-
this.Refresh();
56+
Refresh(timer, listBox);
9857
}
58+
9959
/// <summary>
10060
/// 根据名称添加时间受限的进程
10161
/// </summary>
10262
/// <param name="name">进程名称</param>
10363
/// <param name="limitTime">限制时长(秒)</param>
104-
public void AddByName(string name, int limitTime)
64+
public void AddByName(string name, int limitTime, Timer timer, ListBox listBox)
10565
{
10666
timer.Stop();
10767
Process[] processes = Process.GetProcessesByName(name);
@@ -116,47 +76,48 @@ public void AddByName(string name, int limitTime)
11676
{
11777
MessageBox.Show("错误", ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Error);
11878
}
119-
this.Refresh();
79+
Refresh(timer, listBox);
12080
}
81+
12182
/// <summary>
12283
/// 跟踪所有进程,增加一秒
12384
/// </summary>
12485
public void Run()
12586
{
126-
fileStream.SetLength(0);
12787
foreach (App app in apps)//计算进程时间
12888
{
12989
if (Process.GetProcessesByName(app.Name).Length != 0)
13090
{
13191
if (app is LimitedApp)
13292
{
13393
LimitedApp limitedApp = app as LimitedApp;
134-
limitedApp.Run(streamWriter);
94+
limitedApp.Run();
13595
}
13696
else
137-
app.Run(streamWriter);
97+
app.Run();
13898
}
13999
}
140-
streamWriter.Flush();
141100
}
101+
142102
/// <summary>
143103
/// 移除所列表所选的进程
144104
/// </summary>
145-
public void Remove()
105+
public void Remove(Timer timer, ListBox listBox)
146106
{
147107
timer.Stop();
148108
if (listBox.SelectedIndex >= 0)
149109
apps.RemoveAt(listBox.SelectedIndex);
150-
Refresh();
110+
Refresh(timer, listBox);
151111
}
112+
152113
/// <summary>
153114
/// 删除所有监控
154115
/// </summary>
155-
public void RemoveAll()
116+
public void RemoveAll(Timer timer, ListBox listBox)
156117
{
157118
timer.Stop();
158119
apps.Clear();
159-
Refresh();
120+
Refresh(timer, listBox);
160121
}
161122
}
162-
}
123+
}

TimeControl/ControlPanel.cs

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using System.Runtime.InteropServices;
1111
using System.Diagnostics;
1212
using System.Threading;
13+
using System.Runtime.Serialization.Formatters.Binary;
1314
using System.IO;
1415

1516
namespace TimeControl
@@ -20,6 +21,7 @@ public partial class ControlPanel : Form
2021
private bool isClosable = false;//指示当前是否可以关闭
2122
private string unlockPasswordHash = "";//密码哈希值,用作比对
2223
private AppController controller;//列表、计时控制器
24+
2325
public ControlPanel(bool hide)
2426
{
2527
InitializeComponent();
@@ -29,7 +31,17 @@ public ControlPanel(bool hide)
2931
unlockPasswordHash = File.ReadAllText(TimeControlFile.PassLocation);
3032
PasswordSet();
3133
}
32-
controller = new(usageBox, processMonitorTimer);
34+
BinaryFormatter formatter = new BinaryFormatter();
35+
if (File.Exists(TimeControlFile.TimeFileLocation))
36+
{
37+
using (Stream stream = File.OpenRead
38+
(TimeControlFile.TimeFileLocation))
39+
{
40+
controller = (AppController)formatter.Deserialize(stream);
41+
}
42+
}
43+
else
44+
controller = new(usageBox, processMonitorTimer);
3345
}
3446

3547
private void StartButton_Click(object sender, EventArgs e)//启动屏保程序
@@ -72,7 +84,7 @@ private void ControlPanel_FormClosing(object sender, FormClosingEventArgs e)//
7284
private void ExitToolStripMenuItem_Click(object sender, EventArgs e)//正常退出程序
7385
{
7486
PasswordInput passwordInput = new(unlockPasswordHash);
75-
if (!string.IsNullOrEmpty( unlockPasswordHash))//检测是否设置了管理码
87+
if (!string.IsNullOrEmpty(unlockPasswordHash))//检测是否设置了管理码
7688
{
7789
if (passwordInput.ShowDialog() == DialogResult.OK)
7890
ForceClose();
@@ -93,7 +105,7 @@ private void AppAddButton_Click(object sender, EventArgs e)//添加进程
93105
{
94106
return;
95107
}
96-
TimeInput timeInput = new(controller, processNameBox.Text);//打开进程限时控制窗口
108+
TimeInput timeInput = new(controller, processNameBox.Text, processMonitorTimer, usageBox);//打开进程限时控制窗口
97109
timeInput.ShowDialog();
98110
}
99111

@@ -102,32 +114,44 @@ private void RemoveButton_Click(object sender, EventArgs e)//移除窗口
102114
//检测密码设置
103115
if (PasswordCheck())
104116
{
105-
controller.Remove();
117+
controller.Remove(processMonitorTimer, usageBox);
106118
}
107119
}
108120

109121
private void RefreshButton_Click(object sender, EventArgs e)//重新获取所有软件所用时间
110122
{
111-
controller.Refresh();
123+
UpdateForm();
124+
}
125+
126+
private void UpdateForm()
127+
{
128+
controller.Refresh(processMonitorTimer, usageBox);
112129
}
113130

114131
private void ProcessMonitorTimer_Tick(object sender, EventArgs e)
115132
{
116133
controller.Run();
117134
if (autoRefreshBox.Checked)
118-
controller.Refresh();
135+
UpdateForm();
119136
if (Process.GetProcessesByName("TimeControlConsole").Length == 0)//检查保护程序状态
120137
{
121138
ProcessStartInfo process = new();
122139
process.FileName = "TimeControlConsole.exe";
123140
Process.Start(process);
124141
}
142+
using (Stream stream = File.Create(TimeControlFile.TimeFileLocation))
143+
{
144+
BinaryFormatter formatter = new();
145+
formatter.Serialize(stream, controller);
146+
}
125147
}
148+
126149
private void ForceClose()//可以正常关闭
127150
{
128151
isClosable = true;
129152
Close();
130153
}
154+
131155
private void ControlPanel_Shown(object sender, EventArgs e)//启动隐藏参数支持
132156
{
133157
if (hide)
@@ -137,13 +161,14 @@ private void ControlPanel_Shown(object sender, EventArgs e)//启动隐藏参数
137161

138162
processMonitorTimer.Start();
139163
}
164+
140165
private void UnloackPasswordSetButton_Click(object sender, EventArgs e)//保存密码
141166
{
142-
143-
unlockPasswordHash = Password.ComputeHash( unlockPasswordBox.Text);//保存哈希值
167+
unlockPasswordHash = Password.ComputeHash(unlockPasswordBox.Text);//保存哈希值
144168
File.WriteAllText(TimeControlFile.PassLocation, unlockPasswordHash.ToString());//保存哈希值到文件
145169
PasswordSet();
146170
}
171+
147172
private void PasswordSet()//密码设置后调用
148173
{
149174
unlockPasswordBox.Text = "";
@@ -155,12 +180,13 @@ private void ClearButton_Click(object sender, EventArgs e)//移除所有的已
155180
{
156181
if (PasswordCheck())
157182
{
158-
controller.RemoveAll();
183+
controller.RemoveAll(processMonitorTimer, usageBox);
159184
}
160185
}
186+
161187
private bool PasswordCheck()//检测密码是否正确
162188
{
163-
if (!string.IsNullOrEmpty( unlockPasswordHash))
189+
if (!string.IsNullOrEmpty(unlockPasswordHash))
164190
{
165191
PasswordInput passwordInput = new(unlockPasswordHash);
166192
if (passwordInput.ShowDialog() == DialogResult.OK)
@@ -172,4 +198,4 @@ private bool PasswordCheck()//检测密码是否正确
172198
return true;
173199
}
174200
}
175-
}
201+
}

0 commit comments

Comments
 (0)