Skip to content

Commit 174da0e

Browse files
committed
修改控制台显示逻辑
1 parent 6710101 commit 174da0e

File tree

4 files changed

+381
-370
lines changed

4 files changed

+381
-370
lines changed

ArchivePasswordTestTool.csproj

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,30 @@
1212
<NeutralLanguage>zh-Hans</NeutralLanguage>
1313
<RepositoryUrl>https://github.com/dawn-lc/ArchivePasswordTestTool</RepositoryUrl>
1414
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
15-
<AssemblyVersion>1.0.10.0</AssemblyVersion>
16-
<FileVersion>1.0.10.0</FileVersion>
17-
<Version>1.0.10</Version>
15+
<AssemblyVersion>1.0.12.0</AssemblyVersion>
16+
<FileVersion>1.0.12.0</FileVersion>
17+
<Version>1.0.12</Version>
1818
<PackageIcon></PackageIcon>
1919
<SignAssembly>false</SignAssembly>
2020
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
2121
<PackageLicenseExpression></PackageLicenseExpression>
2222
<RunPostBuildEvent>Always</RunPostBuildEvent>
23-
<ApplicationIcon>favicon-20190111022740169.ico</ApplicationIcon>
23+
<ApplicationIcon>icon.ico</ApplicationIcon>
2424
</PropertyGroup>
2525

2626
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
2727
<PlatformTarget>AnyCPU</PlatformTarget>
2828
<TreatWarningsAsErrors>false</TreatWarningsAsErrors>
29+
<DebugType>none</DebugType>
30+
<DebugSymbols>false</DebugSymbols>
2931
</PropertyGroup>
3032

3133
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
3234
<PlatformTarget>AnyCPU</PlatformTarget>
3335
<WarningLevel>5</WarningLevel>
36+
<DefineConstants>DEBUG;TRACE</DefineConstants>
37+
<DebugType>portable</DebugType>
38+
<DebugSymbols>true</DebugSymbols>
3439
</PropertyGroup>
3540

3641
<ItemGroup>

ConsoleExpandModule.cs

Lines changed: 136 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
using System;
2+
using System.Collections;
3+
using System.Collections.Concurrent;
24
using System.Collections.Generic;
5+
using System.Linq;
36
using System.Threading;
7+
using System.Threading.Tasks;
48

59
namespace module.dawnlc.me
610
{
@@ -9,96 +13,151 @@ namespace module.dawnlc.me
913
/// </summary>
1014
class ConsoleExpand : IDisposable
1115
{
12-
private bool isDisposed = false;
13-
private bool isDisposing = false;
14-
private bool RenderCacheLock = false;
15-
/// <summary>
16-
/// Console 高
17-
/// </summary>
18-
public static int High = Console.WindowHeight;
19-
/// <summary>
20-
/// Console 宽
21-
/// </summary>
22-
public static int Width = Console.WindowWidth;
23-
24-
public static Thread RenderThread;
25-
26-
public static List<Content> RenderCache1 = new List<Content>();
27-
public static List<Content> RenderCache2 = new List<Content>();
28-
29-
public class Content
16+
public class Render : IDisposable
3017
{
31-
public int Row { get; set; }
32-
public int Col { get; set; }
33-
public string ContentString { get; set; }
34-
public Content(int row, int col, string content)
18+
public class Content
3519
{
36-
Row = row;
37-
Col = col;
38-
ContentString = content;
39-
}
40-
}
41-
42-
public ConsoleExpand()
43-
{
44-
Console.SetCursorPosition(0,0);
45-
Console.Clear();
46-
RenderThread = new Thread(OutputCanvas)
47-
{
48-
IsBackground = true
49-
};
50-
RenderThread.Start();
51-
}
52-
53-
public void OutputCanvas()
54-
{
55-
while (!isDisposing)
56-
{
57-
RenderCacheLock = true;
58-
List<Content> Rendering = new List<Content>(RenderCache1);
59-
foreach (var item in Rendering)
20+
public int Row { get; set; }
21+
public int Col { get; set; }
22+
public bool Cover { get; set; }
23+
public string ContentString { get; set; }
24+
public Content(int row, int col, string content,bool cover = true)
6025
{
61-
if (item != null)
62-
{
63-
Console.SetCursorPosition(0, item.Row);
64-
Console.Write(new string(' ', Width));
65-
Console.SetCursorPosition(item.Col, item.Row);
66-
Console.Write(item.ContentString);
67-
}
68-
26+
Row = row;
27+
Col = col;
28+
Cover = cover;
29+
ContentString = content;
6930
}
70-
Rendering.Clear();
71-
RenderCache1.Clear();
31+
}
32+
public class RenderCache : ConcurrentQueue<Content>
33+
{
34+
public ConcurrentQueue<Content> Contents { get; set; }
35+
public bool CacheLock { get; set; }
7236

73-
RenderCache1.AddRange(RenderCache2);
74-
RenderCacheLock = false;
37+
public object SyncRoot => ((ICollection)Contents).SyncRoot;
7538

76-
RenderCache2.Clear();
39+
public bool IsSynchronized => ((ICollection)Contents).IsSynchronized;
7740

78-
Thread.Sleep(32);
41+
public RenderCache()
42+
{
43+
CacheLock = false;
44+
Contents = new ConcurrentQueue<Content>();
45+
}
46+
public void Lock()
47+
{
48+
CacheLock = true;
49+
}
50+
public void Unlock()
51+
{
52+
CacheLock = false;
53+
}
7954
}
80-
}
81-
82-
public void Print(int Col, int Row, string Content)
83-
{
84-
try
55+
private bool isDisposed = false;
56+
public int High = Console.WindowHeight;
57+
public int Width = Console.WindowWidth;
58+
public int Left = Console.CursorLeft;
59+
public int Top = Console.CursorTop;
60+
public Task RenderTask { get; set; }
61+
public CancellationTokenSource RenderTaskTokenSource { get; set; }
62+
public List<RenderCache> Pool { get; set; }
63+
public int RenderInterval { get; set; }
64+
public void Push(Content content)
8565
{
86-
if (RenderCacheLock)
66+
try
8767
{
88-
RenderCache2.Add(new Content(Row, Col, Content));
68+
Pool.Where(p => !p.CacheLock).First().Enqueue(content);
8969
}
90-
else
70+
catch (Exception ex)
9171
{
92-
RenderCache1.Add(new Content(Row, Col, Content));
72+
throw new Exception("缺少可用的输出缓存区!"+ex.ToString());
9373
}
9474
}
95-
catch (Exception)
75+
public Render(int FPS = 10,int PoolCount = 2)
9676
{
97-
//这里如果报错,肯定是破解速度过快.(跑完了7zip test并返回了结果只花了不到10毫秒)
98-
//不过不影响,漏显示一行不算什么大问题(笑
99-
//摸了
77+
RenderInterval = 1000 / FPS;
78+
Pool = new List<RenderCache>();
79+
for (int i = 0; i < PoolCount; i++)
80+
{
81+
Pool.Add(new RenderCache());
82+
}
83+
RenderTaskTokenSource = new CancellationTokenSource();
84+
RenderTask = Task.Factory.StartNew(() => {
85+
try
86+
{
87+
while (true)
88+
{
89+
foreach (var item in Pool.Where(p => p.Count != 0))
90+
{
91+
item.Lock();
92+
for (int i = 0; i < item.Count; i++)
93+
{
94+
if (item.TryDequeue(out Content content))
95+
{
96+
if (content.Cover)
97+
{
98+
Console.SetCursorPosition(0, content.Row);
99+
Console.Write(new string(' ', Width));
100+
}
101+
Console.SetCursorPosition(content.Col, content.Row);
102+
Console.Write(content.ContentString);
103+
}
104+
}
105+
item.Unlock();
106+
}
107+
RenderTaskTokenSource.Token.ThrowIfCancellationRequested();
108+
Thread.Sleep(RenderInterval);
109+
}
110+
}
111+
catch (OperationCanceledException)
112+
{
113+
//外部取消
114+
return;
115+
}
116+
catch (Exception ex)
117+
{
118+
throw ex;
119+
}
120+
}, RenderTaskTokenSource.Token);
121+
}
122+
public void Close()
123+
{
124+
Dispose(true);
125+
}
126+
~Render()
127+
{
128+
Dispose(false);
129+
}
130+
void IDisposable.Dispose()
131+
{
132+
Dispose(true);
133+
GC.SuppressFinalize(this);
134+
}
135+
protected virtual void Dispose(bool disposing)
136+
{
137+
if (!isDisposed)
138+
{
139+
if (disposing)
140+
{
141+
//释放
142+
RenderTaskTokenSource.Cancel();
143+
RenderTask.Wait();
144+
Console.SetCursorPosition(Left,Top);
145+
}
146+
}
147+
isDisposed = true;
100148
}
101149
}
150+
151+
private bool isDisposed = false;
152+
private Render RenderTask { get; set; }
153+
public ConsoleExpand()
154+
{
155+
RenderTask = new Render(25);
156+
}
157+
public void Print(int row, int col, string content)
158+
{
159+
RenderTask.Push(new Render.Content(col, row, content));
160+
}
102161
public void Close()
103162
{
104163
Dispose(true);
@@ -118,33 +177,12 @@ protected virtual void Dispose(bool disposing)
118177
{
119178
if (disposing)
120179
{
121-
RenderThread.Abort();
122-
isDisposing = true;
123-
foreach (var item in RenderCache1)
124-
{
125-
if (item != null)
126-
{
127-
Console.SetCursorPosition(0, item.Row);
128-
Console.Write(new string(' ', Width));
129-
Console.SetCursorPosition(item.Col, item.Row);
130-
Console.Write(item.ContentString);
131-
}
132-
}
133-
foreach (var item in RenderCache2)
134-
{
135-
if (item != null)
136-
{
137-
Console.SetCursorPosition(0, item.Row);
138-
Console.Write(new string(' ', Width));
139-
Console.SetCursorPosition(item.Col, item.Row);
140-
Console.Write(item.ContentString);
141-
}
142-
}
143-
144-
isDisposing = false;
180+
//释放
181+
RenderTask.Close();
145182
}
146183
}
147-
isDisposed = true; // 标识此对象已释放
184+
isDisposed = true;
148185
}
186+
149187
}
150188
}

0 commit comments

Comments
 (0)