In my C# WindowsForm Application, I'm trying to animate a progress bar from start to finish, using a fixed duration (In my case 6500 milliseconds). This is roughly how long the SQL Execute takes.
I've tried running the animate method prior to executing the SQL, but while the SQL is executing, it appears as thought the whole thread is frozen up. I don't want to run the SQL Asynchronously in case of an exception.
I'm new to multi-threading and backgroundworker process, although I believe this may be the key?
If anyone can steer me in the right direction it would be much appreciated!
private void btnInternalMovement_Click(object sender, EventArgs e)
{
AnimateProgBar(6500);
// ***** BLAH MORE CODE HERE ******
using (SqlCommand cmd = new SqlCommand("CatamacInternalStockMovement_C", SqlConnection))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@ItemName", items);
cmd.Parameters.AddWithValue("@Quantity", quantities);
cmd.Parameters.AddWithValue("@WarehouseFrom", cbxFromWarehouse.SelectedValue.ToString());
cmd.Parameters.AddWithValue("@WarehouseTo", cbxToWarehouse.SelectedValue.ToString());
cmd.Parameters.AddWithValue("@LocationFrom", cbxFromZone.SelectedValue.ToString());
cmd.Parameters.AddWithValue("@LocationTo", cbxToZone.SelectedValue.ToString());
cmd.Parameters.AddWithValue("@UOM", "Each");
cmd.Parameters.AddWithValue("@TransferDate", DateTime.Now);
cmd.Parameters.AddWithValue("@User", User);
cmd.ExecuteNonQuery();
}
// PROGRESS BAR ONLY STARTS ANIMATING HERE
MessageBox.Show("Success!");
}
And here is the code for timer / Animate method.
private void timer1_Tick(object sender, EventArgs e)
{
if (progressBar1.Value < 100)
{
progressBar1.Value += 1;
progressBar1.Refresh();
}
else
{
timer1.Enabled = false;
}
}
public void AnimateProgBar(int milliSeconds)
{
if (timer1.Enabled) return;
progressBar1.Value = 0;
timer1.Interval = milliSeconds / 100;
timer1.Enabled = true;
}