?
Solved

NEWBIE: Where does a delegated method "occur"?

Posted on 2007-07-24
6
Medium Priority
?
219 Views
Last Modified: 2010-05-18
Dear Experts,

I'm a C# newbie trying to understand events.  Please take this example:

I have a Clock class, which declares an event.  I have a DisplayTime class, which registers a method with the Clock class event. (Lifted from O'Reilly's Programming C#, thank you.)

When that method executes, is it correct to think of it as the Clock class calling a method inside of the DisplayTime class?  Or is it better to think of it as the Clock method itself executing a chunk of code that happened to come from the DisplayTime class originally?

Specifically, can the method refer to instance and class variables inside of the original DisplayTime class, which are not defined in the method itself?  

Yours in Slowly-Subsiding Confusion,
BrianMc1958
0
Comment
Question by:BrianMc1958
  • 3
  • 2
6 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 1600 total points
ID: 19560092
A little bit of both!

The method inside DisplayTime being run will actually execute in the thread that the Clock class raised the event from.  This may be different from your main UI thread (if that is what DisplayTime is) and require the use of Invoke()/Delegates to marshal the call to the correct UI thread where it is safe to update your GUI components.

But...the method is still encapsulated by the DisplayTime class and can only refer to any members that it could normally access as if it were run from DisplayTime itself.  Many times though, an event will send a reference to the object that generated the event (often called "sender") via event parameters so that the method being executed can access the state of the object that raised the event.
0
 
LVL 12

Assisted Solution

by:renjurdevan
renjurdevan earned 400 total points
ID: 19562621
we can say delegate as typed function pointer!! ie in C++ we cant distingush a function pointer either with the return type or with parametes!! but in c# we can distigush. Delegates are class wchich has ability to store the signature alone!


if we points a function either static or instance it will be called when attached events fired or called!! You have chaining of delegate which calls methods in queueing manner!! all the function should be similar in signature which is same as delegate signature!!

Regards
Renju
0
 

Author Comment

by:BrianMc1958
ID: 19564789
It's the next morning.  The coffee isn't ready yet.  I'm still pondering this.  Thanks...
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:BrianMc1958
ID: 19566622
OK.  I think that answers that.  So the DisplayTime method CAN update instance variables in DisplayTime, even though it may be running in the Clock thread?  Is that right?  (We'll leave HOW that might happen for a much later question...)

If I could ask a follow-up, I had noticed that the sender typically passes back a reference to itself ("this").  I didn't know if that was simply to tell the receiver which class responded, or if it was intended that the receiver would actually use that reference to access public methods and variables in the sender.  If so, why return EventArgs (did I spell that right?) as a separate response?

Given that I'm loading on more of a question, I'll double the points, too...
0
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 1600 total points
ID: 19567980
"So the DisplayTime method CAN update instance variables in DisplayTime, even though it may be running in the Clock thread?"

Correct.

You just need to be aware of what types of variables you should and should not update from a separate thread.  Otherwise, you have to do the cross-thread dance.  This has a whole slew of other considerations such as race-conditions, thread synchronization, atomic operations, etc...  all dependent upon that apps particular scenario.  =)

"We'll leave HOW that might happen for a much later question..."

This generally only occurs if the Clock class is directly creating another thread...or indirectly by using a threaded Timer (not the one from the ToolBox!).

"I didn't know if that was simply to tell the receiver which class responded, or if it was intended that the receiver would actually use that reference to access public methods and variables in the sender."

That is completely dependent upon the architecture of the application...it is possible to send "null" back thru the sender so you should always check it first.  You can declare an event that returns NO parameters at all as well...

"If so, why return EventArgs (did I spell that right?) as a separate response?"

That actually is more a factor of the way the .Net framework itself is setup with OOP in mind and just the way they created the "base" event.  You can derive a class from EventArgs to pass in your event.  This is simply to give "quick" access to some fields you think are relevant to your event.  For instance, the MouseDown() event tells you what button was pressed.
0
 

Author Comment

by:BrianMc1958
ID: 19568555
As is frequently the case, I am very grateful you EE folks would take the time to answer my question/s so clearly and completely.

--BrianMc1958
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Loops Section Overview
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question