Здравствуйте. Есть код, работает в один поток, прошу помочь с работой в несколько потоков.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using MySql.Data.MySqlClient;
using System.Threading;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public MySqlConnection conn()
{
string Connect = "Database=codes;Data Source=xxxxxxxxxx;User Id=xxxxxxxx;Password=xxxxxxxx";
MySqlConnection myConnection = new MySqlConnection(Connect);
myConnection.Open();
return myConnection;
}
public Dictionary<string, int> gen()
{
string str = "1234567890abcdefghijklmnopqrstuvwxyz";
string chr = "1234567890abcdefghijklmnopqrstuvwxyz";
string ints = "1234567890";
if (this.checkBox1.Checked == true && this.checkBox2.Checked == true)
{
str = chr + ints;
}
else if (this.checkBox1.Checked == true)
{
str = chr;
}
else if (this.checkBox2.Checked == true)
{
str = ints;
}
int code_length = Convert.ToInt32(this.numericUpDown1.Value);
var codes_count = this.numericUpDown2.Value;
this.toolStripProgressBar1.Maximum = Convert.ToInt32(codes_count);
byte code_separartor = 0; //$code_separartor=0;
var tmp = new Dictionary<string, int>();
int str_length = str.Length - 1; //$str_length=strlen($str)-1;
Random random = new Random();
while (tmp.Count < codes_count)
{
string code = "";
for (int i = 0; i < code_length; i++)
{
if (i > 0 && code_separartor > 0 && i % code_separartor == 0)
{
code += '-';
}
code += str.Substring(random.Next(0, str_length), 1); //$code.=substr($str, mt_rand(0,$str_length), 1);
}
tmp[code] = 1;
}
var codes = tmp.Keys;
return tmp;
}
public bool send_data(string code_key, MySqlConnection a)
{
string CommandText = "INSERT INTO codes (code, active, `delete`) values ('" + code_key + "', 1, 0)";
MySqlCommand myCommand = new MySqlCommand(CommandText, a);
myCommand.ExecuteNonQuery();
return true;
}
/* */ public void add_to_base(/*Dictionary<string,int> tmp*/)
{
var step = 1;
var tmp = gen();
var a = conn();
var pot = 0;
foreach (var pkey in tmp)
{
send_data(pkey.Key, a);
toolStripProgressBar1.Value = step;
step++;
}
MessageBox.Show("Операция завершена успешно.");
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
richTextBox1.Enabled = true;
}
else
{
richTextBox1.Enabled = false;
}
}
private void button1_Click_1(object sender, EventArgs e)
{
toolStripStatusLabel1.Text = "Генерация";
add_to_base();
toolStripStatusLabel1.Text = "Завершено";
}
}
}