473,651 Members | 2,792 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 ControlUpdateDe legate(long number);

private void ControlUpdater( long number)
{
if (CountTextBox.I nvokeRequired) {
this.BeginInvok e(new
ControlUpdateDe legate(ControlU pdater),
number);
} else {
CountTextBox.Te xt = 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 7235
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 ControlUpdateDe legate(long number);

private void ControlUpdater( long number)
{
if (CountTextBox.I nvokeRequired) {
this.BeginInvok e(new
ControlUpdateDe legate(ControlU pdater),
number);
} else {
CountTextBox.Te xt = 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!F4A38E96E59 8161E!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*******@hotm ail.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 ControlUpdateDe legate(long number);

private void ControlUpdater( long number)
{
if (CountTextBox.I nvokeRequired) {
this.BeginInvok e(new
ControlUpdateDe legate(ControlU pdater),
number);
} else {
CountTextBox.Te xt = 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!F4A38E96E59 8161E!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: "MethodInvo ker 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
11276
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" Src="WebControls/Header.ascx" %> The web user control contains the following server controls
9
5816
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 replies. Since there is no way for a service to post to a client, I am using a thread on the client that polls the server for updates. (Feel free to recommend changes to that as well)
13
3876
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 not have access to that particular tab? > > I tried setting the enabled = false for the given tab, but > it only seems to work in the form assembly section (that > is, if I do it after the form is loaded through a > function, it doesn't seem...
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 grayed out) I am successful to get Media Play Com to work alright in the form but can not get Web Browser to work.
7
3788
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, Delegate); using the standard .Add method of the Dictionary, passing in the name of the delegate instance for the value. private void myMethodBody()
5
3448
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 Cross-Thread exception... Some one can help me? any workaround? Thank you in advance.
7
2721
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
1349
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 provided that I must use this call somehow? Regards Gomez
0
8802
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8697
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8465
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7297
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6158
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5612
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4144
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2699
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 we have to send another system
2
1587
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.