468,161 Members | 1,918 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,161 developers. It's quick & easy.

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 ` ` oo

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 1574
"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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Ant | last post: by
1 post views Thread by Quimbly | last post: by
5 posts views Thread by Larry Smith | last post: by
1 post views Thread by somequestion | last post: by
1 post views Thread by gcdp | last post: by
reply views Thread by kamranasdasdas | last post: by
reply views Thread by gcreed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.