H
Harlan Messinger
I have a list of files that implements IEnumerable<string>, and I am
trying to list them to a listbox on a Windows form:
private void GetFiles()
{
int count = 0;
files = new LazyFileList(@"w:\", "*.asp");
foreach (string file in files)
{
if (canceled)
{
System.Windows.Forms.MessageBox.Show("Enumeration canceled");
break;
}
string status = "" + ++count + " of " + files.Count + " processed";
BeginInvoke(new MethodInvoker(delegate() { UpdateScreen(file,
status); }));
}
}
private void UpdateScreen(string file, string status)
{
textBox1.AppendText(file + System.Environment.NewLine);
label1.Text = status;
label1.Refresh();
}
What's happening in the form is that for most of the files in any given
directory, the name of the LAST file in that directory is displayed, but
the status seems to be getting updated normally.
With a breakpoint on the BeginInvoke line, the Locals window shows me,
on successive iterations:
file = w:\\file01.asp, status = 1 of 37 processed
file = w:\\file02.asp, status = 2 of 37 processed
file = w:\\file03.asp, status = 3 of 37 processed
etc.
With a breakpoint on the call to textBox1.AppendText, the value of
status proceeds sequentially as above, but the file names shown don't
match. The same name will appear multiple times, and then it will jump
ahead to another one:
file = w:\\file14.asp, status = 1 of 37 processed
file = w:\\file14.asp, status = 2 of 37 processed
file = w:\\file14.asp, status = 3 of 37 processed
file = w:\\file14.asp, status = 4 of 37 processed
file = w:\\file35.asp, status = 5 of 37 processed
file = w:\\file35.asp, status = 6 of 37 processed
etc.
I can think of some ideas as to why the values of file and status as
they are at the time I call BeginInvoke aren't the ones that will be
used when the invoker invokes the delegate. But then I don't understand
why only the file name manifests the problem, and the status is fine.
I actually don't want to create an anonymous delegate and then create a
MethodInvoker around *it* anyway, but I don't know a more compact way to
handle the invocation.
trying to list them to a listbox on a Windows form:
private void GetFiles()
{
int count = 0;
files = new LazyFileList(@"w:\", "*.asp");
foreach (string file in files)
{
if (canceled)
{
System.Windows.Forms.MessageBox.Show("Enumeration canceled");
break;
}
string status = "" + ++count + " of " + files.Count + " processed";
BeginInvoke(new MethodInvoker(delegate() { UpdateScreen(file,
status); }));
}
}
private void UpdateScreen(string file, string status)
{
textBox1.AppendText(file + System.Environment.NewLine);
label1.Text = status;
label1.Refresh();
}
What's happening in the form is that for most of the files in any given
directory, the name of the LAST file in that directory is displayed, but
the status seems to be getting updated normally.
With a breakpoint on the BeginInvoke line, the Locals window shows me,
on successive iterations:
file = w:\\file01.asp, status = 1 of 37 processed
file = w:\\file02.asp, status = 2 of 37 processed
file = w:\\file03.asp, status = 3 of 37 processed
etc.
With a breakpoint on the call to textBox1.AppendText, the value of
status proceeds sequentially as above, but the file names shown don't
match. The same name will appear multiple times, and then it will jump
ahead to another one:
file = w:\\file14.asp, status = 1 of 37 processed
file = w:\\file14.asp, status = 2 of 37 processed
file = w:\\file14.asp, status = 3 of 37 processed
file = w:\\file14.asp, status = 4 of 37 processed
file = w:\\file35.asp, status = 5 of 37 processed
file = w:\\file35.asp, status = 6 of 37 processed
etc.
I can think of some ideas as to why the values of file and status as
they are at the time I call BeginInvoke aren't the ones that will be
used when the invoker invokes the delegate. But then I don't understand
why only the file name manifests the problem, and the status is fine.
I actually don't want to create an anonymous delegate and then create a
MethodInvoker around *it* anyway, but I don't know a more compact way to
handle the invocation.