Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Progress bar question

Status
Not open for further replies.

dghundt

Programmer
Apr 28, 2006
28
0
0
US
I am having troubles with progress bars.

This example below works but really is not the behavior I need.

This form allows me to pick an appointment date with the first method, getting the people for that day, then the next two methods fill in the details for an appointment.





Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace CheckApptCalendar
{
public partial class Form1 : Form
{
public string apptDate;
public string database;
public string database1; // one practice
public string database2; // other practice
 
public Form1()
{
InitializeComponent();
dateTimePicker1.Value = (DateTime.Now);
}
public void button1_Click(object sender, EventArgs e)
{
smoothProgressBar1.Value = 0;
ThreadStart threadstart = dowork;
threadstart.BeginInvoke(null, null);
}
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
apptDate = dateTimePicker1.Value.ToString("yyyyMMdd");
}
public void smoothProgressBar1_Load(object sender, EventArgs e)
{
}
public void UpdateProgressBar()
{
if (smoothProgressBar1.InvokeRequired)
{
MethodInvoker updateProgressBar = UpdateProgressBar;
smoothProgressBar1.Invoke(updateProgressBar);
}
else
{
// i just put 5 in to see it work
smoothProgressBar1.Value = smoothProgressBar1.Value + 5;  
}
}
public void dowork()
{
ClassThatDoesWork classthatdoeswork1 = new ClassThatDoesWork();
database1 = "database1";
UpdateProgressBar();
database2 = "database2";
apptDate = dateTimePicker1.Value.ToString("yyyyMMdd"); 
classthatdoeswork1.GetNumberOfAppointments(apptDate, database1, database2);
UpdateProgressBar();
classthatdoeswork1.FillResultsArraywithDetails(apptDate, database1);
UpdateProgressBar();
classthatdoeswork1.FillResultsArraywithDetails(apptDate, database2);
UpdateProgressBar();
ResultsView results = new ResultsView();
results.DisplayResultsTable(classthatdoeswork1.resultsTable);
results.ShowDialog();
}
}
}



What this sample above gives me is a rough progress bar as I go through each of the methods for ClassThatDoesWork. This is a crude estimate however.



What I cannot figure out is how to update the progress bar while I am doing work inside the individual methods of ClassThatDoesWork. For example, when I FillResultsArraywithDetails, I would like to update the progress bar each time I iterate through an appointment (get name 1 - update progress by 5%, get name 2 - update progress to 10%, now get appt1 details - update to 15%, appt2 details complete - update to 20%, .... until I'm done.) The percentages are arbitrary here, but it makes the example simpler.



thanks!
 
I see that the data access is handled as a background thread. Typically it is bad practice, and in most cases not able, to update UI elements from a background task.

It is best practice to create events and have your background task fire off these events which your UI (main program) thread can handle and update the progress bar as required. You can create your own eventargs which can include how much to increment the progress bar.

Hope this puts you in the right direction.

Cheers,

G.

Oxigen - Next generation business solutions
-----------------------------
Components/Tools/Forums/Software/Web Services
 
The Gorkem way using events is working.
If you want another robust solution,use BackgroundWorker class that has all events you need to upsate the progress bar. See ProgessChamged event.
You also find examples on MSDN.
obislavu
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top