I have a program which is updating a table in sqlserver, I have a form which I want to show the progress of this, the progress bar is incrementing but this is not being displayed. do i need to use background worker for this? example of what im doing
public void updateTable(string tableName)
{
// con is an instance of my form to access progressbar
con.progressBar1.Minimum = 1;
con.progressBar1.Step = 1;
string dbQuery = "select summet from someting"
con.progressBar1.Maximum = address.Tables[0].Rows.Count;
MessageBox.Show("progress bar max " + con.progressBar1.Maximum);
foreach (DataRow LonLat in address.Tables[0].Rows)
{
con.progressBar1.PerformStep();
MessageBox.Show(con.progressBar1.Value.ToString()); // this is incrementing
//plus updating table
}
}
Short answer, yes. If you do it from the main thread you won’t see the progress bar update.
Long answer:
Whenever you modify the user interface from a piece of your code that’s translated into a “message” into the window message queue that will get that message and update the UI accordingly in the main thread.
However, since the main thread is busy handling your piece of code it doesn’t have the “time” to actually update the user interface, and only when your process is done, it’s free to update the user interface. That’s why you see the progress bar going from 0% to 100% without any intermediate steps.
What you should do:
What you wanna do is put the work into a background worker, that way the main thread is free to attend UI update requests… that’s, by the way, a standard practice if you wanna keep the UI responsive.