![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| 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 Specs![]() |
| | #2 (permalink) |
| 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 Specs![]() |
| | #3 (permalink) |
| 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 Specs![]() |
| | #4 (permalink) |
| 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? 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 Specs![]() |
| | #5 (permalink) |
| Member | 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 Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Hosting Powershell in App - Obtaining delegates to powershellfunctions | PowerShell | |||
| List delegates in exchange | PowerShell | |||