PC Review


Reply
Thread Tools Rate Thread

Control access: Anonymous Methods, MethodInvoker

 
 
_DD
Guest
Posts: n/a
 
      25th May 2006
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?

 
Reply With Quote
 
 
 
 
Galcho[MCSD.NET]
Guest
Posts: n/a
 
      25th May 2006
I am still using code similar to yours to access controls from thread.
MSDN says: "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."
hope this helps
Galin Iliev[MCSD.NET]
www.galcho.com

 
Reply With Quote
 
Larry Lard
Guest
Posts: n/a
 
      25th May 2006

_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...)

--
Larry Lard
Replies to group please

 
Reply With Quote
 
Galcho[MCSD.NET]
Guest
Posts: n/a
 
      25th May 2006
you're right. it is more elegant

Regards
Galin Iliev[MCSD.NET]

 
Reply With Quote
 
_DD
Guest
Posts: n/a
 
      25th May 2006
On 25 May 2006 02:06:09 -0700, "Larry Lard" <(E-Mail Removed)>
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.




 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Help with anonymous methods Göran Andersson Microsoft C# .NET 1 2nd Sep 2008 04:00 AM
Control.Invoke and anonymous methods Lee Crabtree Microsoft Dot NET Framework Forms 1 26th Aug 2007 11:33 PM
Trying to use anonymous methods to invoke code on a control =?Utf-8?B?QnJhZA==?= Microsoft Dot NET Compact Framework 1 4th Mar 2007 03:19 PM
Fun with anonymous methods :) William Stacey [MVP] Microsoft C# .NET 1 1st Mar 2006 07:08 PM
using anonymous methods Alexandre Microsoft ASP .NET 7 8th Feb 2006 03:08 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:27 AM.