473,811 Members | 3,135 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

delegate confusion

hello world.

i would like to implement a class with a timer, witch informs me every
second about it's tick. the code works already, but i would like to
change a thing (or more).

<code>
//at the moment i initialize the object with the constructor:
public myClass(System. Timers.ElapsedE ventHandler CallMeBack)
{
System.Timers.T imer myTimer = new System.Timers.T imer(1000);
myTimer.Elapsed += CallMeBack;
myTimer.Enabled = True;
}

//the call from outside (other class) looks like:
myClass myObject = new myClasst(new
System.Timers.E lapsedEventHand ler(this.Answer ingMachine));

//with...
private void AnsweringMachin e(object sender,
System.Timers.E lapsedEventArgs e)
{
//THAT'S WHAT I WOULD LIKE TO DO:
//someString = ((myClass)sende r).getValue();
}
</code>

now to my problem (i commented it already in the code):
sender returns the timer and elapsedeventarg s returns the time. i need
access to the calling myObject and it would be very neat to change the
sender and/or the arguments. is there a good solution? i've tried to
inherit from timer but failed, because delegates and events are really
virgin soil for me, so i get confused ` ` o¿o ´ ´

i nearly forgot: i used this implementation, because the polling from
myClass would work completly independent. but would it be wiser to set
a timer in the "outer space" instead of "myClass"?

talked enough.

thanks for putting on the thinking caps...
rené

Jul 26 '07
11 1834
Peter Duniho <Np*********@nn owslpianmk.comw rote:
On Thu, 26 Jul 2007 12:43:29 -0700, Jon Skeet [C# MVP] <sk***@pobox.co m>
wrote:
I think that was probably a different Invoke - like Control.Invoke,
etc. Or possibly it was Delegate.Dynami cInvoke.

Here's the article:
http://forums.microsoft.com/msdn/Sho...66938&SiteID=1

The reply was from a "moderator MVP", and it appears to me that the
messages are specifically about Invoke on a delegate instance. But I may
have misunderstood.
It's a pretty confused message, frankly. Calling a method directly is
faster than calling a delegate at all, but using the C# syntax shortcut
is the same as calling Invoke. It doesn't help that the poster brought
in Control.Invoke, which is *very* different from calling Invoke
directly on the delegate.
The Invoke method for an individual delegate type is declared by the
delegate itself, with appropriate parameters - which is why it isn't in
the Delegate class.

Makes sense to me. Obviously, that should be exactly the same as just
calling the delegate as a method.
Sort of. I think we're on the same page, but just to check... It's
faster to do:

Foo();

than

ThreadStart t = new ThreadStart(Foo );
t();

.... but the latter is exactly the same as:

ThreadStart t = new ThreadStart(Foo );
t.Invoke();

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jul 26 '07 #11
On Thu, 26 Jul 2007 13:13:36 -0700, Jon Skeet [C# MVP] <sk***@pobox.co m>
wrote:
>Makes sense to me. Obviously, that should be exactly the same as just
calling the delegate as a method.

Sort of. I think we're on the same page, but just to check... It's
faster to do:

Foo();
Yes. By "as a method" I simply meant using the syntax that makes the
delegate look like a regular method.

Pete
Jul 26 '07 #12

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

Similar topics

9
1479
by: Wiktor Zychla | last post by:
Hello, I wonder why the delegate declaration needs named parameters? public delegate void MyDelegate( int a, int b ); // ok public delegate void MyDelegate( int, int ); // compiler error C allows to define both: typedef void (*MyDelegate)(int); // ok
8
4545
by: Phill | last post by:
All the event handlers seem to pass an Object and an EventArgs object. If the event doesn't need this info why pass them anyway? It is inefficient.
3
2620
by: Minh Khoa | last post by:
Please give me more information about delegate and its usage? Why do i use it and when?
7
1779
by: Ant | last post by:
Hello, Very simple question but one I need clarified. Which part of the statement below is considered the 'delegate'? Is it the 'new System.EventHandler' or the btnAccept_Click? or is it the piece of code which doesn't appear here, but abstracts the btnAccept_Click method?
1
1827
by: Quimbly | last post by:
I'm having some problems comparing delegates. In all sample projects I create, I can't get the problem to occur, but there is definitely a problem with my production code. I can't give all the code, as there's simply too much, but here's the general gist: I have a connection object which connects to a custom back-end server of one type or another. Clients of this connection object send requests via a method (e.g....
5
29802
by: atwomey | last post by:
When I try and place a delegate in an interface, like this : public interface ITest { double foo(); delegate void bar(); } I get an error "delegates cannot declare types". What is it about a delegate that makes it incompatible in an interface. I know that an interface cannot have any implementation - is there some implementation
5
457
by: Larry Smith | last post by:
Hi there, Given a delegate that points to a non-static member function, is there a cleaner way to change the object instance it's called on other than invoking "Delegate.CreateDelegate()" (and changing the object instance while leaving the same member function intact). Thanks in advance.
1
2410
by: somequestion | last post by:
i'd like to use delegate but it is not in same assembly. how should i do to solve this... source code is like below... using B; namespace A { class A { funcA()
7
2040
by: Dave | last post by:
I've got these declarations: public delegate void FormDisplayResultsDelegate<Type>(Type displayResultsValue); public FormDisplayResultsDelegate<stringdisplayMsgDelegate; instantiation: displayMsgDelegate = DisplayStatusMessage; implementation: public void DisplayStatusMessage(string statusMessage)
0
9722
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9603
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10379
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
10393
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
10124
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9200
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...
0
6882
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
5550
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...
2
3863
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.