Say below that your class is responsible for doing work with the loop. Add an event to indicate your progress. Then, from your user interface, simply handle this event and update the progress bar accordingly.
sealed class Looper
{
public event EventHandler ProgressUpdated;
private int _progress;
public int Progress
{
get { return _progress; }
private set
{
_progress = value;
OnProgressUpdated();
}
}
public void DoLoop()
{
_progress = 0;
for (int i = 0; i < 100; ++i)
{
Thread.Sleep(100);
Progress = i;
}
}
private void OnProgressUpdated()
{
EventHandler handler = ProgressUpdated;
if (handler != null)
{
handler(this, EventArgs.Empty);
}
}
}
You can implement this by specifying BackgroundWorkeras part of your user interface where in the event backgroundWorker.DoWorkyou are calling looper.DoLoop(). Then in the event handler looper.ProgressUpdatedyou can call backgroundWorker.ReportProgressto increase your progress bar from the user interface thread.
, , , , ProgressUpdated ( EventArgs, : , , ).
, , . UI , ( 0 100, ).
, .