Hej. napotkałem mały problem. Chciałem zrobić tryb cichy w rozwiązaniu z poprzedniego wątku, z możliwością wyświetlenia statusu, albo przełączenia trybów cichy/informacyjny.
Nie mogę uporać się z tym problemem. Zrobiłem coś takiego (wybrałem tylko istotnie kawałki kodu). Pętla w PutRandomPassTo7z startuje, ale MonitorKeypress nie przechwytuje klawisza. Co jest nie tak??? MonitorKeypress zaczerpnąłem z jakiegoś forum.
using System;
using System.Security.Cryptography;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.IO;
using System.Diagnostics;
using System.Threading.Tasks;
namespace RandomPassword
{
class Program
{
public static int TaskCount = 6;
public static int Processes7z = 0;
public static int DisplayMode = 0;
static void Main(string[] args)
{
Task.Run(() => PutRandomPassTo7z(MyTaskCount)).ConfigureAwait(false);
Console.ReadLine();
}
private static async void PutRandomPassTo7z(int NewTaskCount)
{
Console.WriteLine("Cracking started...");
var cancelSource = new CancellationTokenSource();
Task consoleKeyTask = Task.Run(() => { MonitorKeypress(cancelSource.Token); });
await consoleKeyTask;
List<Task> ListOfTasks = new List<Task>();
for (int i = 1; i < 100000001; i++)
{
ListOfTasks.Add(Task.Run(async () => await ExtractFile(MainDir, ZipDir, ToDecode, MyDest, MyPass, PassCount, RepsCount, MySpeed, "Random").ConfigureAwait(false)));
while (ListOfTasks.Count >= TaskCount)
{
await Task.WhenAny(ListOfTasks);
ListOfTasks.RemoveAll(x => x.IsCompleted);
}
}
}
}
private static async Task ExtractFile(string MainDir, string ZipDir, string sourceArchive, string MyDest, string MyPasss, int PassCount, int PassRep, int MySpeed, string FileOrRandom)
{
try
{
ProcessStartInfo pro = new ProcessStartInfo();
pro.WindowStyle = ProcessWindowStyle.Hidden;
pro.UseShellExecute = false;
pro.RedirectStandardError = true;
pro.RedirectStandardOutput = true;
pro.FileName = ZipDir;
pro.Arguments = string.Format("t \"{0}\" -p\"{1}\" -y -o\"{2}\"", sourceArchive, MyPasss, MyDest);
//x or t, t is much faster, and password is saved to file (it is not necessary to decrypt file)
Process x = await Task.Run(() => Process.Start(pro)).ConfigureAwait(false);
//x.WaitForExit();
string errorOutput = x.StandardError.ReadToEnd();
string standardOutput = x.StandardOutput.ReadToEnd();
if (x.ExitCode == 0)
{
//save password to file
}
if (x.ExitCode == 2)
{
switch (Program.DisplayMode)
{
case 1:
Console.WriteLine("Something"); //Continous WriteLine
break;
case 2:
Console.WriteLine("Something"); //When in "silent" mode, print once and switch to silent mode
DisplayMode = 3;
break;
case 3:
DisplayMode = 3; //Silent mode
break;
}
}
}
catch (System.Exception Ex)
{
SysEx++;
Console.WriteLine(Ex.Message);
}
}
//------------------------------------------------------------------------------------Display Mode
public static void MonitorKeypress(CancellationToken cancellationToken)
{
ConsoleKeyInfo cki = new ConsoleKeyInfo();
do
{
// true hides the pressed character from the console
cki = Console.ReadKey();
switch (cki.KeyChar)
{
case '1':
Program.DisplayMode = 1;
break;
case '2':
Program.DisplayMode = 2;
break;
case '3':
Program.DisplayMode = 3;
break;
}
// Wait for an ESC
} while (cki.Key != ConsoleKey.Escape);
// Cancel the token
CancellationTokenSource source = new CancellationTokenSource();
source.Cancel();
}
}
}