How to increase a variable every time I create a new Thread and store that value? I want to do mysqlbackup in each thread for different databases, and I will get the curBytes/totalBytes completed on Class1.cs. I want to update that value into datagridview.Row[iterate].Cell[2] simultaneously between threads where iterate corresponds with value 0 for first thread, 1 for second thread and so on.
I have tried using Interlocked.Increment and lock() but they seem not to work. I don't want to use BackgroundWorker.
This is the code that I haven't done anything to change datagridIterate (the value that iterate through datagridview Rows)
Form.cs:
private void button1_Click(object sender, EventArgs e)
{
restoreUI();
}
public void restoreUI()
{
mysqlUtils msu = new mysqlUtils();
foreach(DataGridViewRow row in dataGridView1.Rows)
{
Thread t = new Thread(() =>
{
msu.Restore(row.Cells[1].Value?.ToString(), server.Text, username.Text,
pwd.Text, row.Cells[0].Value?.ToString());
}
t.Start();
}
}
Class1.cs:
namespace Utils
{
public class mysqlUtils
{
public int total;
public int cur;
public void Restore(string filename, string server, string user,
string password, string db)
{
string constring = "server=" + server + ";user=" + user +
";pwd=" + password + ";database=" + db + ";";
string file = "C:\\Users\\SQL FILES\\" + filename;
using (MySqlConnection conn = new MySqlConnection(constring))
{
using (MySqlCommand cmd = new MySqlCommand())
{
using (MySqlBackup mb = new MySqlBackup(cmd))
{
cmd.Connection = conn;
conn.Open();
mb.ImportInfo.IntervalForProgressReport = 50;
mb.ImportProgressChanged += updateProgress;
mb.ImportFromFile(file);
conn.Close();
}
}
}
}
}
public void updateProgress(object sender, ImportProgressArgs e)
{
total = (int)e.TotalBytes;
cur = (int)e.CurrentBytes;
Form4 form4 = (Form4)Application.OpenForms[0];
form4.updateProgressValue(total, cur);
}
}
How should I manage to change datagridIterate that will have value i for first thread, i+1 for the next thread and so on? I would highly appreciate if you have any support.
Here's a big hint for your code that might help you to understand what you need.
Try this:
And your
MySqlUtilsclass.NB: I've changed the naming conventions to standard C#.