Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > .NET General

Vista Tutorial - Delegates help

Reply
 
Old 07-25-2008   #1 (permalink)
Miro
Guest


 
 

Delegates help

> 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" <miro@xxxxxx> wrote in message
news:OqTPHYm7IHA.3976@xxxxxx
Quote:

>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

My System SpecsSystem Spec
Old 07-25-2008   #2 (permalink)
Adam Benson
Guest


 
 

Re: Delegates help

> 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" <miro@xxxxxx> wrote in message
news:OqTPHYm7IHA.3976@xxxxxx
Quote:

>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

My System SpecsSystem Spec
Old 07-25-2008   #3 (permalink)
Miro
Guest


 
 

Re: Delegates help

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 MyFirstDelegation(ByVal blaTxt As String)


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim deleg As MyFirstDelegation
deleg = New MyFirstDelegation(AddressOf DisplayMessage)

deleg.Invoke("Why 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
=====

My System SpecsSystem Spec
Old 07-25-2008   #4 (permalink)
Jon Skeet [C# MVP]
Guest


 
 

Re: Delegates help

Miro <miro@xxxxxx> wrote:
Quote:

> 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 - <skeet@xxxxxx>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
My System SpecsSystem Spec
Old 07-25-2008   #5 (permalink)
Member


Join Date: May 2008
Vista Business x64
 
 

Re: Delegates help

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.
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Hosting Powershell in App - Obtaining delegates to powershellfunctions PowerShell
List delegates in exchange PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46