473,785 Members | 2,488 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Delegates help

I am trying to understand delegates and I think I do understand them ...
just hoping if someone can tell me im on the right track.
So far what I have read is that a Delegate is an Asynchronus call to a
method or function or whatever...

So basically you create an object that references the address of the
meathod.

By calling this delegate - you get a 'return' right away so your code can
continue and the delegate runs in a different thread doing whatever the
function / meathod is required.

Is this the correct assumption?
The example in the book is pretty simple ( its a writeline command ).

So as far as 'uses' for delegates ( if im on the right track )...its useful
when you have a lot to process, but do not want to do it in the main thread
( or the app will temporarily stop working ), you use a delegate to call the
meathod in a different thread.

Is this correct?

Thanks,

Miro

Jul 25 '08 #1
4 1298
Is this correct?
Not at all

I don't know if you're familiar with C or C++ (ignore this sentence if you
aren't) but if you are then the closest to them that you're familiar with
are function pointers.

In C# I guess you could think of them as a variable for method calls. Like
variables they have a type, and can be instantiated. An instance of a
delegate type can be called (invoked) and that will result in the method the
delegate instance points at being called.

So a delegate has a signature or type describing the kind of method it can
point to. A piece of code may say "If you want to know when I've done this
then I'll tell you - give me a delegate of such and such a type and I'll
call it when I've done it."

The bit about delegates executing on another thread - well, I don't know
where that came from, but a delegate call will execute on the same thread
that called it.

This is a bit of a loose definition, but I hope it gets you started.

Best way to figure this out is just to play with it. Create a form with a
button on and aim to pop up a message box when the button is clicked on.
That involves the use of delegates. Or type "delegate" in Visual Studio and
hit F1. That will call up the help for it.

Cheers,

Adam.
"Miro" <mi**@beero.com wrote in message
news:Oq******** ******@TK2MSFTN GP06.phx.gbl...
>I am trying to understand delegates and I think I do understand them ...
just hoping if someone can tell me im on the right track.
So far what I have read is that a Delegate is an Asynchronus call to a
method or function or whatever...

So basically you create an object that references the address of the
meathod.

By calling this delegate - you get a 'return' right away so your code can
continue and the delegate runs in a different thread doing whatever the
function / meathod is required.

Is this the correct assumption?
The example in the book is pretty simple ( its a writeline command ).

So as far as 'uses' for delegates ( if im on the right track )...its
useful when you have a lot to process, but do not want to do it in the
main thread ( or the app will temporarily stop working ), you use a
delegate to call the meathod in a different thread.

Is this correct?

Thanks,

Miro

Jul 25 '08 #2
I might still be missing something but I see how the delegate points to the
function/method :

But I still seem to be missing why I wouldnt call the sub "DisplayMessage "
directly? Why delegate it?

Thanx,

Miro

=====
Public Class Form1

Delegate Sub MyFirstDelegati on(ByVal blaTxt As String)
Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click
Dim deleg As MyFirstDelegati on
deleg = New MyFirstDelegati on(AddressOf DisplayMessage)

deleg.Invoke("W hy not just call display message directly")

Debug.WriteLine ("when did this line get hit?")
End Sub

Sub DisplayMessage( ByVal msgText As String)
MessageBox.Show (msgText, "delegation ")
End Sub

End Class
=====

Jul 25 '08 #3
Miro <mi**@beero.com wrote:
I might still be missing something but I see how the delegate points to the
function/method :

But I still seem to be missing why I wouldnt call the sub "DisplayMessage "
directly? Why delegate it?
In this case, you wouldn't. The point is to be able to effectively pass
some logic to other pieces of code.

See http://csharpindepth.com/Articles/Ch.../Closures.aspx for an
article which concentrates on closures, but should also give you a
flavour of why delegates are a good thing.

--
Jon Skeet - <sk***@pobox.co m>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Jul 25 '08 #4

The part about executing on another thread is probably an incomplete
reference to the fact that when an event is raised from a thread other
than the main GUI thread the event handler executes in the context of
that thread. If the event handler has to update controls owned by the
GUI thread you need to use a delegate and Invoke method to punt the
execution of the handler code that updates the controls to the GUI
thread (otherwise you will likely get a cross-threading exception).
Summary: delegates can, in combination with the Invoke method, pass
execution of the delegate method's code to the main GUI thread from
another thread.
--
breitak67
Jul 26 '08 #5

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

Similar topics

6
2293
by: Jeffrey T. Smith | last post by:
Back when the new J2SE1.5 features were announced, there was a JavaLive community chat (http://java.sun.com/developer/community/chat/JavaLive/2003/jl0729.html) in which Neal Gafter explains the Sun stance on lack of support for delegates: .... There are serious semantic problems with trying to add delegates to a language in a consistent way. The main problem is that once you call the delegate, the original class instance is no longer...
1
324
by: Pablo Salazar | last post by:
Hi people. I found a book, it talked about oop, in a chapter i found a topic called "DELEGATES", I read it , but I didn't Understand. Somebody can tell me where I can find a tutorial or book that explain delegates in a easy way. Thanks!. Pablo Salazar.
4
22891
by: LP | last post by:
Hello! I am still transitioning from VB.NET to C#. I undertand the basic concepts of Delegates, more so of Events and somewhat understand AsyncCallback methods. But I need some clarification on when to use one over another? If anyone could provide any additional info, your comments, best practices, any good articles, specific examples, etc. Thank you
14
3265
by: Lior Amar | last post by:
Quick question about threads and delegates. I have the following scenario Thread A (CLASSA) spawns Thread B (CLASSB) and passes it a DelegateA to a callback Thread B Invokes a DelegateB asynchronously (could be a timer but then we get Thread C) Upon completion of DelegateB, Thread B would like to callback ThreadA using DelegateA but as we all know the call to DelegateA is running in ThreadB. Is
4
1876
by: AMDRIT | last post by:
I am trying to understand Delegates and where/when to use them. I can see one potential use of a delegate (on form closing, set the cancel property in the event arguments.) Does anyone have a link to a site that describes delegates using VB.Net in a manner that doesn't require rocket science to become enlightened. TIA
5
3833
by: vbgunz | last post by:
Hello everyone. I own two books. Learning Python and Python in a nutshell. When cross referencing the two books to try and clarify the ideas behind extending methods and delegates, this is where confusion veered it's ugly head :( Learning Python explains on page 324: Class Interface Techniques (21.3.3 in the ebook) the following is an extender method. ''' #################################### '''
6
2663
by: =?Utf-8?B?T2xkQ2FEb2c=?= | last post by:
My question is regarding the use of delegates in C#. I see how .Net uses delegates to wire event handlers to events. It’s an object created by a single line of code by the system and that makes perfect sense to me. I understand that there is a lot of code underneath that the system has created that makes it all work, thereby making it pretty efficient for the programmer. Outside of the use of delegates to wire event handlers, you can...
69
5594
by: raylopez99 | last post by:
They usually don't teach you in most textbooks I've seen that delegates can be used to call class methods from classes that are 'unaware' of the delegate, so long as the class has the same signature for the method (i.e., as below, int Square (int)). Here is an example to show that feature. Note class "UnAwareClass" has its methods Square and Cuber called by a class DelegateClass. This is because these methods in UnAwareClass have the...
3
4847
by: David K in San Jose | last post by:
I'm using managed (CLR) C++ in VS2005 to create a Windows app that contains a form named "MyForm". In the code for that form I'm trying to invoke some static functions by using an array of function pointers (delegates). I assume I need to use the array< T keyword to allocate an array of delegates, and then initialize the array by setting each array element to the pointers (handles) of the functions I'll be invoking. I've been trying to...
0
9480
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
10325
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10147
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
10091
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,...
1
7499
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.