Note that there are some explanatory texts on larger screens.

plurals
  1. POwhy is progress bar not updated when the main UI is running by using backgroundworker? c#.net
    primarykey
    data
    text
    <p>Can anyone enlighten me on this? It's updated until the main UI thread is completed, why is that so?</p> <p>Part of my code:</p> <p>//Initialize the BW</p> <pre><code> 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); } </code></pre> <p>\Do Work event</p> <pre><code> private void BW_DoWork(object sender, DoWorkEventArgs e) { BackgroundWorker worker = sender as BackgroundWorker; for (int i = CurrentProcess; i &lt;= Process; i++) { if (worker.CancellationPending == true) { e.Cancel = true; break; } else { System.Threading.Thread.Sleep(100); worker.ReportProgress(i); } } CurrentProcess = Process; } </code></pre> <p>\Update main UI</p> <pre><code> private void BW_ProgressChanged(object sender, ProgressChangedEventArgs e) { pgBar.Value = e.ProgressPercentage; lbProgress.Text = (e.ProgressPercentage.ToString() + "%"); pgBar.Refresh(); lbProgress.Refresh(); } </code></pre> <p>\My main UI Operation</p> <pre><code>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 { } } </code></pre> <p>//Call to create database and table</p> <pre><code>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; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload