-1
private void MMScrpt()
{
    try
    {
       MySqlConnectionStringBuilder connBuilder = new MySqlConnectionStringBuilder();

       connBuilder.Add("Database", databaseMM);
       connBuilder.Add("Data Source", "localhost");
       connBuilder.Add("User Id", "root");
       connBuilder.Add("Password", txtPassword.Text);

       MySqlConnection connection = new MySqlConnection(connBuilder.ConnectionString);
       MySqlCommand cmd = connection.CreateCommand();

       connection.Open();
       progressBar1.Minimum = 0;
       progressBar1.Step = 1;
       progressBar1.Value = 0;

       string MMScript = Properties.Resources.millmancompany;
       string[] tokens = MMScript.Split(';');
       progressBar1.Maximum = tokens.Length;
       for (int i = 0; i < tokens.Length; i++)
       {
           MMtokens = tokens[i];
           cmd.CommandText = MMtokens.ToString();
           cmd.CommandType = CommandType.Text;
           cmd.ExecuteNonQuery();
           progressBar1.PerformStep();
       }
       connection.Close();
       connection.Close();
   }

   catch (Exception ex)
   {
       MessageBox.Show(ex.Message, "Error Message");
   }
}

private void BTNsriptRUN_Click(object sender, EventArgs e)
{

    Thread tr1 = new Thread(new ThreadStart(MMScrpt));

    tr1.Start();
    //MMScrpt();

}
Sal00m
  • 2,938
  • 3
  • 22
  • 33
user3839566
  • 73
  • 1
  • 6
  • possible duplicate of [Cross-thread operation not valid: Control accessed from a thread other than the thread it was created on](http://stackoverflow.com/questions/142003/cross-thread-operation-not-valid-control-accessed-from-a-thread-other-than-the) – leppie Jul 15 '14 at 06:29
  • With the introduction of async/await, you can easily rewrite the code without creating any thread explicitly. Thus, if possible spend some time on that and save your own time. – Lex Li Jul 15 '14 at 09:12

1 Answers1

0

You can not access UI controls on a thread. You should use a dispatcher, or use a Task to update the UI components.

For example, add a reference to windowBase, keep a reference to the current Dispatcher before launching the thread, and the use it to update the controls:

//Reference to CurrentDispatcher before launching thread:

Dispatcher myDispatcher = Dispatcher.CurrentDispatcher;

myDispatcher.BeginInvoke(new Action(() =>
{
 progressBar1.Minimum = 0;
 progressBar1.Step = 1;
 progressBar1.Value = 0;

 }));
Carlos Landeras
  • 11,025
  • 11
  • 56
  • 82