On 25 May 2006 02:06:09 -0700, "Larry Lard" <la*******@hotmail.com>
wrote:
_DD wrote: In the switch-over to C# 2.0, I thought it would be good to get rid of
external delegates for thread access to controls. In looking into
anonymous methods I ran across MethodInvoker. I haven't been able to
come up with code samples that show usage in this context.
Current C# v1 code:
public delegate void ControlUpdateDelegate(long number);
private void ControlUpdater(long number)
{
if (CountTextBox.InvokeRequired) {
this.BeginInvoke(new
ControlUpdateDelegate(ControlUpdater),
number);
} else {
CountTextBox.Text = number.ToString();
}
}
What is the preferred way to do this in C# v2? Does MethodInvoker fit
into the picture, or was that mis-cue?
I like the elegance of this method:
<http://spaces.msn.com/staceyw/blog/cns!F4A38E96E598161E!652.entry>
(this page makes my Firefox hang btw, so use IE...)
Thanks for the link. It probably has what I'm looking for, though
I'll have to do some decoding. On the other hand, this example
doesn't seem to improve readability of code. It was a surprise to get
to the bottom of those functions and see: }).Start();
I think I'd use the separate delegate there, for readability.
IOW, the code I posted above is more readable for me. Maybe because
I'm accustomed to the whole delegate-control-access template by now.
I did get a version of that function running using MethodInvoker, but
it was by trial and error. It looks simpler, but I have not been able
to resolve what little documentation I've found on using MethodInvoker
in this context (including mention of 'No passed parameters').
From MSDN: "MethodInvoker provides a simple delegate that is used to
invoke a method with a void parameter list. This delegate can be used
when making calls to a control's Invoke method, or when you need a
simple delegate but do not want to define one yourself."
Unfortunately I need to pass at least one parameter into most of the
control update functions. But oddly, I've been able to get code
running with MethodInvoker.