473,503 Members | 1,647 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Control access: Anonymous Methods, MethodInvoker

_DD
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?

May 25 '06 #1
4 7231
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

May 25 '06 #2

_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

May 25 '06 #3
you're right. it is more elegant

Regards
Galin Iliev[MCSD.NET]

May 25 '06 #4
_DD
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.


May 25 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
11267
by: martin | last post by:
Hi, I am a web page and a web user control. My web user control is placed in my web page using the following directive <%@ Register TagPrefix="uc1" TagName="Header"...
9
5798
by: Jervin Justin | last post by:
Hi, I've been having this problem for some time with Web Forms: I have a web app that sends data to a service when the user presses a button. Based on the data, the server will send several...
13
3854
by: KMiller | last post by:
I would like to control entry into one or more of the tabs > I have set up on my Tab control based on set rules. Does > anyone know how to grey-out the text so that a user will > see that they do...
28
564
by: BobAchgill | last post by:
Has anyone had success with using this Com control? When I add it into my tool box and drop it into my form it says in the properties that the Document and LocationURL are read only. (they are...
7
3774
by: Bill Woodruff | last post by:
I've found it's no problem to insert instances of named delegates as values into a generic dictionary of the form : private Dictionary<KeyType, DelegatemyDictionary = new Dictionary<KeyType,...
5
3431
by: Ale | last post by:
Hi to everyone, i've a problem, i'm trying to edit a control from a secondary thread (i'm using the backgroundworker) the problem is that i can't edit something in that way.. and i get back a...
7
2711
by: Stefan Hoffmann | last post by:
hi @all, is there something like an anonymous delegate? This is the original code: -- private delegate void DelegateSetFormCaption(string text); private void SetFormCaption(string text) {
3
1346
by: Gomez | last post by:
When I call a method by remoting I get the following exception- "Control 'conrolname' accessed from a thread other than the thread it was created on" Can someone advise how to cope with that...
0
7083
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7278
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7328
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6988
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5011
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3153
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1510
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.