473,405 Members | 2,262 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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.ElapsedEventHandler CallMeBack)
{
System.Timers.Timer myTimer = new System.Timers.Timer(1000);
myTimer.Elapsed += CallMeBack;
myTimer.Enabled = True;
}

//the call from outside (other class) looks like:
myClass myObject = new myClasst(new
System.Timers.ElapsedEventHandler(this.AnsweringMa chine));

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

now to my problem (i commented it already in the code):
sender returns the timer and elapsedeventargs 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 #1
11 1804
"ohmmega" <sh****@gmx.atwrote in message
news:11**********************@k79g2000hse.googlegr oups.com...
now to my problem (i commented it already in the code):
sender returns the timer and elapsedeventargs 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?
One way to do it is to redirect the timer callback into your own routine,
and this routine can in turn call the original callback with the arguments
of your choice, such as "this" for the "sender":
class myClass
{
private System.Timers.ElapsedEventHandler theCallBack;

public myClass(System.Timers.ElapsedEventHandler CallMeBack)
{
this.theCallBack = CallMeBack;
System.Timers.Timer myTimer = new System.Timers.Timer(1000);
myTimer.Elapsed += MyCallback;
myTimer.Enabled = True;
}

private void MyCallback(object sender,
System.Timers.ElapsedEventArgs e)
{
theCallBack(this, e);
}

//(Here goes the rest of class, such as clean-up code for the Timer)
}
Jul 26 '07 #2
ohmmega,

I don't have a compiler on me but I think something along the lines of
this should do what you want. Our anonymous method will handle the
event, and then fire the event handler with myClass as the sender
instead of myTimer.

public myClass(System.Timers.ElapsedEventHandler CallMeBack)
{
System.Timers.Timer myTimer = new System.Timers.Timer(1000);
myTimer.Elapsed += delegate(object sender,
System.Timers.ElapsedEventArgs e)
{
CallMeBack.Invoke(this, e);
};
myTimer.Enabled = True;
}

BTW: You need to make myTimer a member variable or it will get garbage
collected.

Good Luck,
James

Jul 26 '07 #3
On Thu, 26 Jul 2007 07:40:19 -0700, ohmmega <sh****@gmx.atwrote:
now to my problem (i commented it already in the code):
sender returns the timer and elapsedeventargs 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 ´ ´
Alberto has posted one good solution. In his method, the class
initializing the timer essentially makes it look as though it is the class
implementing the timer. It encapsulates the timer itself internally; in
addition to the obvious advantage of addressing your question, it also has
the advantage that the myClass class can change the implementation of the
timer more easily (and even more easily if you change the handler delegate
type to something that myClass defines rather than using the one in the
Timers namespace :) ).

There are at least two other ways I can think of to address the issue,
which may or may not be appropriate for your needs depending on how you
want it to work.

The first involves simply saving the myClass instance reference and using
that. For example:

public ClientClass
{
myClass myObject;

void SomeMethod()
{
myObject = new myClass(AnsweringMachine);
}

void AnsweringMachine(object sender, ElapsedEventArgs e)
{
someString = myObject.getValue();
}
}

Because the delegate includes a reference to the class instance used to
create the delegate, you can then refer to instance members, such as the
"myObject" field, from within the delegate method.

A variation on the above would be to create a simple class in which you
put the delegate method, and which stores the "myObject" instance. That
way you could create a new instance for each instance of myClass that you
create, if that was desirable for some reason. Of course, you would have
to include in that simple class some way to get back to the ClientClass if
you wanted to be able to do things specific to that instance from within
the callback as well. As you can see, this technique can get arbitrarily
complicated if you want. :)

Another way to address the issue would be to use an anonymous delegate:

public ClientClass
{
void SomeMethod()
{
myClass myObject;
ElapsedEventArgs callback = delegate(object sender,
ElapsedEventArgs e)
{ someString = myObject.getValue(); }

myObject = new myClass(callback);
}
}

This takes advantage of the fact that the local variable "myObject" is
"captured" by the anonymous delegate. So you can use the variable within
the code of the delegate, and it will retain the value you set it to in
the SomeMethod() method. This might be especially appropriate if the
callback method exists for the sole purpose of handling that one
situation, which is in fact often the case for this sort of thing.
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"?
I don't think it matters. If you have no need to access the timer from
outside myClass, I would say that you should not store it outside
myClass. If the myClass instance itself has no need to access the timer
either, then I would say there's no need to store it even within myClass.

Pete
Jul 26 '07 #4
On Thu, 26 Jul 2007 10:05:37 -0700, james <ja********@gmail.comwrote:
public myClass(System.Timers.ElapsedEventHandler CallMeBack)
{
System.Timers.Timer myTimer = new System.Timers.Timer(1000);
myTimer.Elapsed += delegate(object sender,
System.Timers.ElapsedEventArgs e)
{
CallMeBack.Invoke(this, e);
};
myTimer.Enabled = True;
}
Any particular reason you prefer the "CallMeBack.Invoke(this, e)" syntax
as opposed to the (IMHO more readable) "CallMeBack(this, e)" syntax? I
believe that in situations where you know the delegate type exactly (as is
the case here), the latter is preferable and somewhat more efficient at
run-time.
BTW: You need to make myTimer a member variable or it will get garbage
collected.
Not true. This code works fine:

static void Start()
{
Timer timer = new Timer();

timer.Elapsed += ElapsedHandler;
timer.Interval = 2000;
timer.Start();
}

static void ElapsedHandler(object sender, ElapsedEventArgs e)
{
Console.WriteLine("timer elapsed");
}

static void Main(string[] args)
{
Start();

GC.Collect();
Console.ReadLine();
}

Pete
Jul 26 '07 #5
Peter Duniho <Np*********@nnowslpianmk.comwrote:
On Thu, 26 Jul 2007 10:05:37 -0700, james <ja********@gmail.comwrote:
public myClass(System.Timers.ElapsedEventHandler CallMeBack)
{
System.Timers.Timer myTimer = new System.Timers.Timer(1000);
myTimer.Elapsed += delegate(object sender,
System.Timers.ElapsedEventArgs e)
{
CallMeBack.Invoke(this, e);
};
myTimer.Enabled = True;
}

Any particular reason you prefer the "CallMeBack.Invoke(this, e)" syntax
as opposed to the (IMHO more readable) "CallMeBack(this, e)" syntax? I
believe that in situations where you know the delegate type exactly (as is
the case here), the latter is preferable and somewhat more efficient at
run-time.
It's perhaps preferable from a readability point of view, but the
compiled code is the same. The syntax you're suggesting is just sugar
provided by the C# compiler.

--
Jon Skeet - <sk***@pobox.com>
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 #6
On Thu, 26 Jul 2007 11:34:34 -0700, Jon Skeet [C# MVP] <sk***@pobox.com>
wrote:
It's perhaps preferable from a readability point of view, but the
compiled code is the same. The syntax you're suggesting is just sugar
provided by the C# compiler.
Okay. I read an article that suggested that because Invoke can be used
without knowing the exact delegate signature, some extra run-time stuff
has to happen to handle that syntax. But maybe that referred to a
different way of using the Invoke() method. Or maybe it was just plain
wrong. :)
Jul 26 '07 #7
Hey Pete,

I tried your code and System.Timers.Timer isn't garbage collected, but
a System.Threading.Timer is garbage collected. Any idea what is going
on here?
static void Start()
{
System.Threading.Timer threadingTimer = new
System.Threading.Timer(delegate
{
Console.WriteLine("System.Threading.Timer");
}, null, 0, 2000);

System.Timers.Timer timersTimer = new
System.Timers.Timer();

timersTimer.Elapsed += delegate
{
Console.WriteLine("System.Timers.Timer");
};
timersTimer.Interval = 2000;
timersTimer.Start();
}
static void Main(string[] args)
{
Start();

GC.Collect();
Console.ReadLine();
}

Thanks,
James

Jul 26 '07 #8
Peter Duniho <Np*********@nnowslpianmk.comwrote:
It's perhaps preferable from a readability point of view, but the
compiled code is the same. The syntax you're suggesting is just sugar
provided by the C# compiler.

Okay. I read an article that suggested that because Invoke can be used
without knowing the exact delegate signature, some extra run-time stuff
has to happen to handle that syntax. But maybe that referred to a
different way of using the Invoke() method. Or maybe it was just plain
wrong. :)
I think that was probably a different Invoke - like Control.Invoke,
etc. Or possibly it was Delegate.DynamicInvoke.

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.

--
Jon Skeet - <sk***@pobox.com>
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 #9
On Thu, 26 Jul 2007 12:43:29 -0700, Jon Skeet [C# MVP] <sk***@pobox.com>
wrote:
I think that was probably a different Invoke - like Control.Invoke,
etc. Or possibly it was Delegate.DynamicInvoke.
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.
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.

Pete
Jul 26 '07 #10
Peter Duniho <Np*********@nnowslpianmk.comwrote:
On Thu, 26 Jul 2007 12:43:29 -0700, Jon Skeet [C# MVP] <sk***@pobox.com>
wrote:
I think that was probably a different Invoke - like Control.Invoke,
etc. Or possibly it was Delegate.DynamicInvoke.

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.com>
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.com>
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
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...
8
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
by: Minh Khoa | last post by:
Please give me more information about delegate and its usage? Why do i use it and when?
7
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...
1
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...
5
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...
5
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...
1
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
by: Dave | last post by:
I've got these declarations: public delegate void FormDisplayResultsDelegate<Type>(Type displayResultsValue); public FormDisplayResultsDelegate<stringdisplayMsgDelegate; instantiation:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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
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
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...
0
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...

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.