I simply want a nondeterministic progressbar that becomes visible and shows some “activity” to indicate a query is running, and then, when the query is through, goes back to being invisible. Seems simple enough; but with this code:
try
{
Cursor.Current = Cursors.WaitCursor;
progressBarChanges.Value = 50;
progressBarChanges.Step = 20;
progressBarChanges.Visible = true;
... // the meat of the code, where the query is being done, elided
} finally
{
progressBarChanges.Visible = false;
Cursor.Current = Cursors.Default;
}
…the progress bar never displays, even though the query takes awhile to run. The progress bar is on a DGV. I realize my progress code is a bit lame, but first things first – I just want the darned thing to show something for starters.
UPDATE
I am setting the progressBar to visible way before I’m calling the BackgroundWorker proc that runs the query:
progressBarChanges.Value = 50;
progressBarChanges.Step = 20;
progressBarChanges.Visible = true;
. . .
if (args.KeyCode == Keys.Enter)
{
if (ValidEntryForCRID(textBoxID.Text))
{
RetrieveAndBindPlatypusData();
var tb = (TextBox)Controls.Find("textBoxDuckbill", true).First();
if (tb != null)
{
tb.Focus();
}
if ((!string.IsNullOrWhiteSpace(textBoxID.Text)) &&
(backgroundWorkerShowChanges.IsBusy != true))
{
backgroundWorkerShowChanges.RunWorkerAsync();
}
. . .
…yet the progressBar is never becoming visible (unless I never set it back to visible = false after the DGV’s DataSource is assigned the OracleDateTable value of the returned query/result set). If I comment that out, it then (belatedly) finally shows up, apparently thinking (erroneously) “better late than never.”
Is there a way to force Windows to “pay attention” to the “progressBarChanges.Visible = true;” line right away? Something like a .ProcessMessages() or this.Refresh or…???
This is how I got it to work:
I enable the ProgressBar just before the BackgroundWorker call:
The BackgroundWorker code gets the OracleDataTable from the DB:
The BackgroundWorker’s “completed” event updates the UI:
In PopulateChangesGrid(), after assigning the OracleDataTable object to the DGV, I set the Progress bar invisible:
It works like a charm (<– not necessarily a reference to Windows 8 “Metro” charms).