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();
}
答案 0 :(得分:0)
您无法访问线程上的UI控件。您应该使用调度程序,或使用任务来更新UI组件。
例如,添加对windowBase的引用,在启动线程之前保持对当前Dispatcher的引用,并使用它来更新控件:
//Reference to CurrentDispatcher before launching thread:
Dispatcher myDispatcher = Dispatcher.CurrentDispatcher;
myDispatcher.BeginInvoke(new Action(() =>
{
progressBar1.Minimum = 0;
progressBar1.Step = 1;
progressBar1.Value = 0;
}));