472,981 Members | 1,447 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,981 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 ` ` 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 1767
"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: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.