Increase a variable in a loop and for each increment, apply for each new thread

660 views Asked by At

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.

1

There are 1 answers

6
Enigmativity On

Here's a big hint for your code that might help you to understand what you need.

Try this:

public class Form4 : Form
{
    private DataGridView dataGridView1 = new DataGridView();
    private TextBox server = new TextBox();
    private TextBox username = new TextBox();
    private TextBox pwd = new TextBox();

    private void button1_Click(object sender, EventArgs e)
    {
        RestoreUI();
    }

    public void RestoreUI()
    {
        MySqlUtils msu = new MySqlUtils();
        foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            string filename = row.Cells[1].Value?.ToString();
            string s = server.Text;
            string user = username.Text;
            string password = pwd.Text;
            string db = row.Cells[0].Value?.ToString();
            Thread t = new Thread(() => msu.Restore(filename, s, user, password, db, UpdateProgress));
            t.Start();
        }
    }

    public void UpdateProgress(object sender, ImportProgressArgs e)
    {
        this.Invoke((Action)(() =>
        {
            /* add your code here to update the UI */
        }));
    }
}

And your MySqlUtils class.

public class MySqlUtils
{
    public void Restore(string filename, string server, string user, string password, string db, ImportProgressChange updateProgress)
    {
        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();
                }
            }
        }
    }
}

NB: I've changed the naming conventions to standard C#.