T
trant
I have a Form with a progress bar and a thread that should update the
progress every quarter of a second. The operation being monitored for
progress on is a file loading job.
So I start off the monitor like this:
long currprogress = 0;
FileInfo info = new FileInfo(currFile);
long maxprogress = info.Length / 100;
bool IsMonitoring = true;
Thread monitor = new Thread(new ThreadStart(RunMonitor));
monitor.Start();
(the scope of these variables is class level though, just made them local
scope for easier reading)
Then my RunMonitor method looks like:
private void RunMonitor()
{
while (IsMonitoring)
{
Thread.Sleep(250);
int i = (int)((double)(currprogress / 100) /
(double)maxprogress * 100);
Console.WriteLine("new Progress "+i);
UpdatePositionDelegate u = new
UpdatePositionDelegate(UpdateProgress);
this.Invoke(u, i);
}
}
That delegate is declared as:
public delegate void UpdatePositionDelegate(int progress);
and the UpdatePorgress method it invokes on the Form (this) is:
private void UpdateProgress(int val)
{
progress.Value = val;
statusStrip1.Refresh();
}
But it's not working.
It goes through RunMonitor once and hangs on the call to UpdateProgress the
whole time the file is loaded, so never updates the progress bar.
What could be wrong? Why does it hang?
progress every quarter of a second. The operation being monitored for
progress on is a file loading job.
So I start off the monitor like this:
long currprogress = 0;
FileInfo info = new FileInfo(currFile);
long maxprogress = info.Length / 100;
bool IsMonitoring = true;
Thread monitor = new Thread(new ThreadStart(RunMonitor));
monitor.Start();
(the scope of these variables is class level though, just made them local
scope for easier reading)
Then my RunMonitor method looks like:
private void RunMonitor()
{
while (IsMonitoring)
{
Thread.Sleep(250);
int i = (int)((double)(currprogress / 100) /
(double)maxprogress * 100);
Console.WriteLine("new Progress "+i);
UpdatePositionDelegate u = new
UpdatePositionDelegate(UpdateProgress);
this.Invoke(u, i);
}
}
That delegate is declared as:
public delegate void UpdatePositionDelegate(int progress);
and the UpdatePorgress method it invokes on the Form (this) is:
private void UpdateProgress(int val)
{
progress.Value = val;
statusStrip1.Refresh();
}
But it's not working.
It goes through RunMonitor once and hangs on the call to UpdateProgress the
whole time the file is loaded, so never updates the progress bar.
What could be wrong? Why does it hang?