任何人都可以启发我吗?直到主UI线程完成才更新,为什么会这样?
我的部分代码:
//初始化BW
private void InitializeBW()
{
BW.WorkerReportsProgress = true;
BW.WorkerSupportsCancellation = true;
BW.DoWork +=
new DoWorkEventHandler(BW_DoWork);
BW.RunWorkerCompleted +=
new RunWorkerCompletedEventHandler(
BW_RunWorkerCompleted);
BW.ProgressChanged +=
new ProgressChangedEventHandler(
BW_ProgressChanged);
}
\做工作事件
private void BW_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
for (int i = CurrentProcess; i <= Process; i++)
{
if (worker.CancellationPending == true)
{
e.Cancel = true;
break;
}
else
{
System.Threading.Thread.Sleep(100);
worker.ReportProgress(i);
}
}
CurrentProcess = Process;
}
\更新主界面
private void BW_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
pgBar.Value = e.ProgressPercentage;
lbProgress.Text = (e.ProgressPercentage.ToString() + "%");
pgBar.Refresh();
lbProgress.Refresh();
}
\我的主要用户界面操作
private void btnCreate_Click(object sender, EventArgs e)
{
try
{
DatabaseParam DBPara = new DatabaseParam();
isCreate = true;
EDFields(!isCreate);
DBPara.ServerName = txtServerName.Text;
DBPara.DatabaseName = "PARKS_ARCHIVE_" + nudYear.Value.ToString();
DBPara.ID = txtUserName.Text;
DBPara.Password = txtPassword.Text;
DBPara.DataFileName = "PARKS_ARCHIVE_DATA_" + nudYear.Value.ToString();
DBPara.DataPathName = txtPath.Text + "\\PARKS_ARCHIVE_DATA_" + nudYear.Value.ToString();
DBPara.DataFileSize = "10MB";
DBPara.DataFileGrowth = "1024MB";
DBPara.LogFileName = "PARKS_ARCHIVE_LOG_" + nudYear.Value.ToString();
DBPara.LogPathName = txtPath.Text + "\\PARKS_ARCHIVE_LOG_" + nudYear.Value.ToString();
DBPara.LogFileSize = "10MB";
DBPara.LogFileGrowth = "20MB";
CreateDatabase(DBPara);
}
catch (Exception ex)
{
Logger.LogError("ucArchivePurge", "btnCreate_Click", ex.Message);
}
finally
{
}
}
//调用创建数据库和表
private void CreateDatabase(DatabaseParam DBParam)
{
try
{
Process = 7;
BW = new BackgroundWorker();
InitializeBW();
if (BW.IsBusy != true)
{
// Start the asynchronous operation.
BW.RunWorkerAsync();
}
System.Threading.Thread.Sleep(800);
txtProgress.Text = "Creating " + DBParam.DatabaseName + "...";
SQLConn.Open();
myCommand = new SqlCommand(sqlCreateDBQuery, SQLConn);
myCommand.ExecuteNonQuery();
SQLConn.Close();
BW.CancelAsync();
Process = 14;
BW = new BackgroundWorker();
InitializeBW();
if (BW.IsBusy != true)
{
// Start the asynchronous operation.
BW.RunWorkerAsync();
}
System.Threading.Thread.Sleep(800);
txtProgress.Text = "Creating Table tblAuditLogin...";
txtProgress.Refresh();
SQLCreate.Open();
myCommand.Connection = SQLCreate;
myCommand.CommandText = CreateAuditLogin;
myCommand.ExecuteNonQuery();
BW.CancelAsync();
Process = 21;
BW = new BackgroundWorker();
InitializeBW();
if (BW.IsBusy != true)
{
// Start the asynchronous operation.
BW.RunWorkerAsync();
}
System.Threading.Thread.Sleep(800);
txtProgress.Text = "Creating Table tblAuditTrail...";
txtProgress.Refresh();
myCommand.CommandText = CreateAuditTrail;
myCommand.ExecuteNonQuery();
BW.CancelAsync();
Process = 28;
BW = new BackgroundWorker();
InitializeBW();
if (BW.IsBusy != true)
{
// Start the asynchronous operation.
BW.RunWorkerAsync();
}
txtProgress.Text = "Creating Table tblCardType...";
txtProgress.Refresh();
System.Threading.Thread.Sleep(800);
myCommand.CommandText = CreateCardType;
myCommand.ExecuteNonQuery();
Process = 35;
BW = new BackgroundWorker();
InitializeBW();
if (BW.IsBusy != true)
{
// Start the asynchronous operation.
BW.RunWorkerAsync();
}
txtProgress.Text = "Creating Table tblErrorLog...";
txtProgress.Refresh();
System.Threading.Thread.Sleep(800);
myCommand.CommandText = CreateErrorLog;
myCommand.ExecuteNonQuery();
Process = 42;
BW = new BackgroundWorker();
InitializeBW();
if (BW.IsBusy != true)
{
// Start the asynchronous operation.
BW.RunWorkerAsync();
}
txtProgress.Text = "Creating Table tblKanban_Card...";
txtProgress.Refresh();
System.Threading.Thread.Sleep(800);
myCommand.CommandText = CreateTableKanban;
myCommand.ExecuteNonQuery();
Process = 49;
BW = new BackgroundWorker();
InitializeBW();
if (BW.IsBusy != true)
{
// Start the asynchronous operation.
BW.RunWorkerAsync();
}
txtProgress.Text = "Creating Table tblModule_Master...";
txtProgress.Refresh();
System.Threading.Thread.Sleep(800);
myCommand.CommandText = CreateTableModule;
myCommand.ExecuteNonQuery();
Process = 56;
BW = new BackgroundWorker();
InitializeBW();
if (BW.IsBusy != true)
{
// Start the asynchronous operation.
BW.RunWorkerAsync();
}
txtProgress.Text = "Creating Table tblMTV...";
txtProgress.Refresh();
System.Threading.Thread.Sleep(800);
myCommand.CommandText = CreateTableMTV;
myCommand.ExecuteNonQuery();
Process = 63;
BW = new BackgroundWorker();
InitializeBW();
if (BW.IsBusy != true)
{
// Start the asynchronous operation.
BW.RunWorkerAsync();
}
txtProgress.Text = "Creating Table tblPickList_Detail...";
txtProgress.Refresh();
System.Threading.Thread.Sleep(800);
myCommand.CommandText = CreateTablePickDetail;
myCommand.ExecuteNonQuery();
Process = 70;
BW = new BackgroundWorker();
InitializeBW();
if (BW.IsBusy != true)
{
// Start the asynchronous operation.
BW.RunWorkerAsync();
}
txtProgress.Text = "Creating Table tblPickList_Header...";
txtProgress.Refresh();
System.Threading.Thread.Sleep(800);
myCommand.CommandText = CreatePickHeader;
myCommand.ExecuteNonQuery();
Process = 77;
BW = new BackgroundWorker();
InitializeBW();
if (BW.IsBusy != true)
{
// Start the asynchronous operation.
BW.RunWorkerAsync();
}
txtProgress.Text = "Creating Table tblProd_Requisition...";
txtProgress.Refresh();
System.Threading.Thread.Sleep(800);
myCommand.CommandText = CreateTableProduction;
myCommand.ExecuteNonQuery();
Process = 84;
BW = new BackgroundWorker();
InitializeBW();
if (BW.IsBusy != true)
{
// Start the asynchronous operation.
BW.RunWorkerAsync();
}
txtProgress.Text = "Creating Table tblUser_Master...";
txtProgress.Refresh();
System.Threading.Thread.Sleep(800);
myCommand.CommandText = CreateTableUser;
myCommand.ExecuteNonQuery();
Process = 91;
BW = new BackgroundWorker();
InitializeBW();
if (BW.IsBusy != true)
{
// Start the asynchronous operation.
BW.RunWorkerAsync();
}
txtProgress.Text = "Creating Table tblUser_Rights...";
txtProgress.Refresh();
System.Threading.Thread.Sleep(800);
myCommand.CommandText = CreateTableRight;
myCommand.ExecuteNonQuery();
SQLCreate.Close();
Process = 100;
BW = new BackgroundWorker();
InitializeBW();
if (BW.IsBusy != true)
{
// Start the asynchronous operation.
BW.RunWorkerAsync();
}
txtProgress.Text = "Updating Log File...";
txtProgress.Refresh();
myCommand.CommandText = Log;
myCommand.Connection = SQLConn;
SQLConn.Open();
myCommand.ExecuteNonQuery();
SQLConn.Close();
if (MessageBox.Show("Database " + DBParam.DatabaseName + " has been successfully created !", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information) == DialogResult.OK)
{
txtProgress.Text = "";
}
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message, "Create Database",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
finally
{
if (SQLConn != null)
SQLConn.Close();
if (SQLCreate != null)
SQLCreate.Close();
isCreate = false;
EDFields(!isCreate);
}
return;
}