En faisant ça tu lances juste ta tache dans un second thread,mais celui-ci sera lancé que dans un seul thread (ton appli sera certe multithread mais pas ta fonction decrypt1), tu as le mot clé parrallel ou tu peux t'amuser à gérer le nombre de thread, sinon t'as d'autre classe et d'autre outils, je pourrais pas tous te les énumérer, j'ai essayé le parrallel.for, il y a pas un gros impact dans ton cas.
Et tu es sur de ton code dans la fonction decrypt ?
Je pense qu'il y a moyen d'améliorer mon code
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Decrypt
{
class Program
{
static string file = @"C:\Users\Eddy\Documents\test.txt";
static char[] alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
public static void Main(String[] args)
{
Console.WriteLine("Started at : " + DateTime.Now);
//Thread t = new Thread(new ThreadStart(ParrallelDecrypt));
//t.Start();
NormalDecrypt();
ParrallelDecrypt();
Console.ReadKey();
}
static String Decrypt(String inputString, String key)
{
int j = 0;
String outputString = "";
int len = inputString.Length;
for (int i = 0; i < len; i++)
{
if (j == 4)
{
j = 0;
}
outputString = outputString + char.ToString((char)(inputString[i] ^ key[j]));
j = j + 1;
}
return outputString;
}
static void NormalDecrypt()
{
Stopwatch sw = new Stopwatch();
sw.Start();
string text = System.IO.File.ReadAllText(file);
for (int c1 = 0; c1 < alpha.Length; c1++)
{
for (int c2 = 0; c2 < alpha.Length; c2++)
{
for (int c3 = 0; c3 < alpha.Length; c3++)
{
for (int c4 = 0; c4 < alpha.Length; c4++)
{
string val = String.Concat(alpha[c1], alpha[c2], alpha[c3], alpha[c4]);
Decrypt(text, val);
}
}
}
}
Console.WriteLine("Normal task finished at : " + sw.ElapsedMilliseconds);
}
static void ParrallelDecrypt()
{
Stopwatch sw = new Stopwatch();
sw.Start();
string text = System.IO.File.ReadAllText(file);
Parallel.For(0, alpha.Length, (c1) =>
{
for (int c2 = 0; c2 < alpha.Length; c2++)
{
for (int c3 = 0; c3 < alpha.Length; c3++)
{
for (int c4 = 0; c4 < alpha.Length; c4++)
{
string val = String.Concat(alpha[c1], alpha[c2], alpha[c3], alpha[c4]);
Decrypt(text, val);
// Decrypt(text, $"{alpha[c1]}{alpha[c2]}{alpha[c3]}{alpha[c4]}");
}
}
}
//Parallel.For(0, alpha.Length, (c2) =>
//{
// Parallel.For(0, alpha.Length, (c3) =>
// {
// Parallel.For(0, alpha.Length, (c4) =>
// {
// Decrypt(text2, "" + alpha[c1] + alpha[c2] + alpha[c3] + alpha[c4]);
// });
// });
//});
});
Console.WriteLine("Parrallel task finished at : " + sw.ElapsedMilliseconds);
}
}
}