Skip to content

Commit

Permalink
修复多线程问题
Browse files Browse the repository at this point in the history
  • Loading branch information
dawn-lc committed May 22, 2021
1 parent f578107 commit 211d0a4
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 41 deletions.
82 changes: 49 additions & 33 deletions ConsoleExpandModule.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;

namespace module.dawnlc.me
Expand All @@ -11,6 +10,7 @@ namespace module.dawnlc.me
class ConsoleExpand : IDisposable
{
private bool isDisposed = false;
private bool isDisposing = false;
private bool RenderCacheLock = false;
/// <summary>
/// Console 高
Expand Down Expand Up @@ -52,21 +52,29 @@ public ConsoleExpand()

public void OutputCanvas()
{
while (true)
while (!isDisposing)
{
RenderCacheLock = true;
IEnumerable<Content> Rendering = RenderCache1.Where(p => p != null);
List<Content> Rendering = new List<Content>(RenderCache1);
foreach (var item in Rendering)
{
Console.SetCursorPosition(0, item.Row);
Console.Write(new string(' ', Width));
Console.SetCursorPosition(item.Col, item.Row);
Console.Write(item.ContentString);
if (item != null)
{
Console.SetCursorPosition(0, item.Row);
Console.Write(new string(' ', Width));
Console.SetCursorPosition(item.Col, item.Row);
Console.Write(item.ContentString);
}

}
RenderCache1 = RenderCache1.Except(Rendering).ToList();
RenderCache1 = RenderCache1.Union(RenderCache2).Where(p => p != null).ToList();
Rendering.Clear();
RenderCache1.Clear();

RenderCache1.AddRange(RenderCache2);
RenderCacheLock = false;

RenderCache2.Clear();

Thread.Sleep(32);
}
}
Expand All @@ -75,22 +83,18 @@ public void Print(int Col, int Row, string Content)
{
try
{
if (!string.IsNullOrWhiteSpace(Content))
if (RenderCacheLock)
{
if (RenderCacheLock)
{
RenderCache2.Add(new Content(Row, Col, Content));
}
else
{
RenderCache1.Add(new Content(Row, Col, Content));
}
RenderCache2.Add(new Content(Row, Col, Content));
}
else
{
RenderCache1.Add(new Content(Row, Col, Content));
}
return;
}
catch (Exception)
{
return;
//这里如果报错,肯定是破解速度过快.(跑完了7zip test并返回了结果只花了不到10毫秒)
}
}
public void Close()
Expand All @@ -108,23 +112,35 @@ void IDisposable.Dispose()
}
protected virtual void Dispose(bool disposing)
{
RenderThread.Abort();
RenderCacheLock = true;
RenderCache1 = RenderCache1.Union(RenderCache2).ToList();
RenderCacheLock = false;
foreach (var item in RenderCache1)
{
Console.SetCursorPosition(0, item.Row);
Console.Write(new string(' ', Width));
Console.SetCursorPosition(item.Col, item.Row);
Console.Write(item.ContentString);
}
if (!isDisposed)
{
if (disposing)
{
RenderCache2.Clear();
RenderCache1.Clear();
RenderThread.Abort();
isDisposing = true;

foreach (var item in RenderCache1)
{
if (item != null)
{
Console.SetCursorPosition(0, item.Row);
Console.Write(new string(' ', Width));
Console.SetCursorPosition(item.Col, item.Row);
Console.Write(item.ContentString);
}
}
foreach (var item in RenderCache2)
{
if (item != null)
{
Console.SetCursorPosition(0, item.Row);
Console.Write(new string(' ', Width));
Console.SetCursorPosition(item.Col, item.Row);
Console.Write(item.ContentString);
}
}

isDisposing = false;
}
}
isDisposed = true; // 标识此对象已释放
Expand Down
22 changes: 16 additions & 6 deletions Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System.Threading.Tasks;
using System.IO;
using module.dawnlc.me;
using System.Threading;

namespace ArchivePasswordTestTool
{
Expand Down Expand Up @@ -371,6 +372,7 @@ private void MainProgram()
if (ProgramParameter.ArchiveFile.Extension.ToLower().Contains("rar"))
{
Console.WriteLine("RAR格式压缩包!由于RAR专利问题需要调用完整版7Zip!");
Console.WriteLine("本程序将会读取注册表中的7Zip安装路径,如果不想程序读取,请直接关闭本程序。");
Console.WriteLine("请确保已安装完整版7zip!(按任意键继续!)");
Console.ReadKey();
if (string.IsNullOrEmpty(ReadRegeditValue("SOFTWARE\\7-Zip", "Path").ToString()))
Expand All @@ -386,8 +388,7 @@ private void MainProgram()
}
}

Dictionary<string, List<string>> returnData = RunProgram(ProgramParameter.ArchiveDecryptionProgram, new string[] { "t", "\"" + ProgramParameter.ArchiveFile.FullName + "\"", "-p" });
if (!returnData.TryGetValue("Output", out List<string> Output))
if (!RunProgram(ProgramParameter.ArchiveDecryptionProgram, new string[] { "t", "\"" + ProgramParameter.ArchiveFile.FullName + "\"", "-p" }).TryGetValue("Output", out List<string> Output))
{
Console.WriteLine("压缩包损坏 或 不是支持的压缩包!(按任意键退出)");
Console.ReadKey();
Expand Down Expand Up @@ -433,10 +434,15 @@ private void MainProgram()
{
Parallel.ForEach(Dictionary, new ParallelOptions() { MaxDegreeOfParallelism = ProgramParameter.DecryptArchiveThreadCount }, (i, loopState) => {
ConsoleCanvas.Print(0, i.Thread, "[" + i.Thread + "] 密码: [" + i.PassWord + "] 测试中...");
returnData = RunProgram(ProgramParameter.ArchiveDecryptionProgram, new string[] { "t", "\"" + ProgramParameter.ArchiveFile.FullName + "\"", "-p\"" + i.PassWord + "\"" });
if (returnData.ContainsKey("Output"))

/*
Dictionary<string, List<string>> returnDataA = new Dictionary<string, List<string>> { ["Output"] = new List<string> { "test" } };
Thread.Sleep(random.Next(2,5));
*/

if (RunProgram(ProgramParameter.ArchiveDecryptionProgram, new string[] { "t", "\"" + ProgramParameter.ArchiveFile.FullName + "\"", "-p\"" + i.PassWord + "\"" }).TryGetValue("Output", out List<string> OutputInfo))
{
if (returnData["Output"].Where(p => p != null && p.Contains("Everything is Ok")).Any())
if (OutputInfo.Where(p => p != null && p.Contains("Everything is Ok")).Any())
{
ProgramParameter.EncryptArchivePassword = i.PassWord;
loopState.Stop();
Expand All @@ -447,9 +453,13 @@ private void MainProgram()
ConsoleCanvas.Print(0, i.Thread, "[" + i.Thread + "] 密码: [" + i.PassWord + "] 错误!");
}
}

else
{
ConsoleCanvas.Print(0, i.Thread, "[" + i.Thread + "] 密码: [" + i.PassWord + "] 错误!");
}
});
}

sw.Stop();
Console.SetCursorPosition(0, ProgramParameter.DecryptArchiveThreadCount + 1);
if (ProgramParameter.EncryptArchivePassword != null)
Expand Down
2 changes: 1 addition & 1 deletion Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class ProgramParameter
{
public static readonly string AppName = Assembly.GetExecutingAssembly().FullName.Substring(0, Assembly.GetExecutingAssembly().FullName.IndexOf(","));
public static readonly string AppPath = Environment.CurrentDirectory + "\\";
public static readonly int[] Version = new int[] { 1, 0, 8 };
public static readonly int[] Version = new int[] { 1, 0, 9 };
public static readonly string VersionType = "Release";
public static readonly string AppHomePage = "https://www.bilibili.com/read/cv6101558";
public static readonly string Developer = "dawn-lc";
Expand Down
3 changes: 2 additions & 1 deletion Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"profiles": {
"ArchivePasswordTestTool": {
"commandName": "Project"
"commandName": "Project",
"commandLineArgs": "-D \"C:\\Users\\lc990\\Desktop\\字典破解\\工具 - 副本\\PasswordDictionary.txt\" -T 7 -F C:\\Users\\lc990\\Desktop\\字典破解\\空压缩包.7z"
}
}
}

0 comments on commit 211d0a4

Please sign in to comment.