SlideShare a Scribd company logo
1 of 9
C# 001
From the Delegate to the Thread
Tom McHugh
tom.mchugh@gmail.com
2009 ver0.01


this:
click HERE to get this presentation

code:
click HERE to get code.
Contents & Why
• Contents
  – delegate creation recipe
• Why
  – delegates are the foundation stone for threading and a
    whole bunch of other things
A delegate and it’s method (block diagram):




a delegate type: "DmyDel"


   an instance of the                              target method called
   delegate named           which will call this   "MTargetMethod"
   "dMyDel"                  target method
Some interesting notes.
The word “delegate” is a keyword in the C# language, so it shows
  up as blue when you write it in the Visual Studio C#
  programming environment.

When you call a method you “invoke” it. I don’t think “Invoke” is a
  keyword in C#, buts it’s a pretty important concept, so,
  remember, when you call a method, you invoke it.

The word “synchronous” is a bit much, it just means “I’m going to
  do what I want, in order – in a very simple order; when I’m told
  and not until I’m told; and I’m probably not going to do anything
  else while I’m doing it. Oh, and I’m not expecting anyone to
  bother me when I’m finished”.

Asynchronous: the antidote to synchronicity is to think in an event
  driven manner: it’s a reactionary term, but programmers use it.
Recipe: creating a delegate:
There is going to be something happening in this program which uses a
    delegate, and the name of that type of delegate will be “DmyDel”.

There is going to be a specific instance of this type of delegate, and I’m
    going to call it “dMyDel”.

I’m going to have a method in my program and I am going to call it using
     this delegate; the methods name will be “MTargetMethod”

Let the delegate instance know which target method it will be using
      (instantiation).

Don’t freak out, it’s simple really, let’s take it slow….

                                                      a delegate type: "DmyDel"


                                                          an instance of the                                target method called
                                                          delegate named             which will call this   "MTargetMethod"
                                                          "dMyDel"                    target method




                                              all in the same thread "Main" (ignore "threading" for now, sorry I mentioned it)
Event diagram. We’ll use this kind of thing a lot …
  creation
                       Main()                                                       MTargetMethod()



               delegate void DMyDel();
                                              so, now, when the delegate is used,
                                                    it will invoke this method
                  DMyDel dMydel;


        dMydel = new DMyDel(MTargetMethod);




  execution
                      dMyDel()                           calls                          dostuff


                                     once the target method is done, then
                                     control returns to the calling method
Recipe: creating a delegate                                       a delegate type: "DmyDel"


                                                                      an instance of the                                target method called
                                                                      delegate named             which will call this   "MTargetMethod"
                                                                      "dMyDel"                    target method




                                                          all in the same thread "Main" (ignore "threading" for now, sorry I mentioned it)


For a delegate, you need the following ingredients:

-    a declaration of the delegate: “delegate void DmyDel();”

     There is going to be something happening in this program which uses a
     delegate, and the name of that type of delegate will be “DmyDel”.

-    a definition of the delegate: “DmyDel dMyDel;”

     There is going to be a specific instance of this type of delegate, and I’m going
     to call it “dMyDel”.

-    create a method which will be the target method for the delegate

     I’m going to have a method in my program and I am going to call it using this
     delegate; the methods name will be “MTargetMethod”

-    instantiate the delegate: “dMyDel = new DmyDel(MTargetMethod);”

     Let the delegate instance know which target method it will be using
     (instantiation).
Code for a simple delegate:
using System.Windows.Forms;


namespace Delegate_01
{
    public partial class Form1 : Form
    {
        delegate void DMyDel();      // creation recipe step 1 of 4: declare a delegate type with a type name of "DMyDel"
        DMyDel dMydel;             // creation recipe step 2 of 4: declare a delegate of that type with a name "dMydel"


        public Form1()
        {
            InitializeComponent(); // this is created by the compiler, not me


            dMydel = new DMyDel(MTargetMethod); // creation recipe step 4 of 4: instantiate the delegate by
        }


        void MTargetMethod()         // creation recipe step 3 of 4: create a maethod which will be the target method of the delegate
        {
            richTextBox1.AppendText("The target method has been calledrn"); // execution recipe 2 of 2: do stuff
        }


        private void button1_Click(object sender, System.EventArgs e)
        {
            dMydel();          // execution recipe step 1 of 2: make the delegate call the method
            dMydel.Invoke();      // execution recipe step 1 of 2: there are loads of ways to do the same thing in C#
        }
    }
}
Coming Soon
• //TODO ……
• simple delegate (synchronous)
• simple delegate (semi-synchronous)
• intermediate delegate (pseudo-synchronous)
• intermediate delegate (ok: asynchronous)
• intermediate delegate (very asynchronous, and an
  extra method is called at the end – that’s called a
  “callback”
• post-intermediate …..
• what next
•

More Related Content

Recently uploaded

Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 

Recently uploaded (20)

Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 

Featured

Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 

Featured (20)

Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 

From Delegates To Threads 01

  • 1. C# 001 From the Delegate to the Thread Tom McHugh tom.mchugh@gmail.com 2009 ver0.01 this: click HERE to get this presentation code: click HERE to get code.
  • 2. Contents & Why • Contents – delegate creation recipe • Why – delegates are the foundation stone for threading and a whole bunch of other things
  • 3. A delegate and it’s method (block diagram): a delegate type: "DmyDel" an instance of the target method called delegate named which will call this "MTargetMethod" "dMyDel" target method
  • 4. Some interesting notes. The word “delegate” is a keyword in the C# language, so it shows up as blue when you write it in the Visual Studio C# programming environment. When you call a method you “invoke” it. I don’t think “Invoke” is a keyword in C#, buts it’s a pretty important concept, so, remember, when you call a method, you invoke it. The word “synchronous” is a bit much, it just means “I’m going to do what I want, in order – in a very simple order; when I’m told and not until I’m told; and I’m probably not going to do anything else while I’m doing it. Oh, and I’m not expecting anyone to bother me when I’m finished”. Asynchronous: the antidote to synchronicity is to think in an event driven manner: it’s a reactionary term, but programmers use it.
  • 5. Recipe: creating a delegate: There is going to be something happening in this program which uses a delegate, and the name of that type of delegate will be “DmyDel”. There is going to be a specific instance of this type of delegate, and I’m going to call it “dMyDel”. I’m going to have a method in my program and I am going to call it using this delegate; the methods name will be “MTargetMethod” Let the delegate instance know which target method it will be using (instantiation). Don’t freak out, it’s simple really, let’s take it slow…. a delegate type: "DmyDel" an instance of the target method called delegate named which will call this "MTargetMethod" "dMyDel" target method all in the same thread "Main" (ignore "threading" for now, sorry I mentioned it)
  • 6. Event diagram. We’ll use this kind of thing a lot … creation Main() MTargetMethod() delegate void DMyDel(); so, now, when the delegate is used, it will invoke this method DMyDel dMydel; dMydel = new DMyDel(MTargetMethod); execution dMyDel() calls dostuff once the target method is done, then control returns to the calling method
  • 7. Recipe: creating a delegate a delegate type: "DmyDel" an instance of the target method called delegate named which will call this "MTargetMethod" "dMyDel" target method all in the same thread "Main" (ignore "threading" for now, sorry I mentioned it) For a delegate, you need the following ingredients: - a declaration of the delegate: “delegate void DmyDel();” There is going to be something happening in this program which uses a delegate, and the name of that type of delegate will be “DmyDel”. - a definition of the delegate: “DmyDel dMyDel;” There is going to be a specific instance of this type of delegate, and I’m going to call it “dMyDel”. - create a method which will be the target method for the delegate I’m going to have a method in my program and I am going to call it using this delegate; the methods name will be “MTargetMethod” - instantiate the delegate: “dMyDel = new DmyDel(MTargetMethod);” Let the delegate instance know which target method it will be using (instantiation).
  • 8. Code for a simple delegate: using System.Windows.Forms; namespace Delegate_01 { public partial class Form1 : Form { delegate void DMyDel(); // creation recipe step 1 of 4: declare a delegate type with a type name of "DMyDel" DMyDel dMydel; // creation recipe step 2 of 4: declare a delegate of that type with a name "dMydel" public Form1() { InitializeComponent(); // this is created by the compiler, not me dMydel = new DMyDel(MTargetMethod); // creation recipe step 4 of 4: instantiate the delegate by } void MTargetMethod() // creation recipe step 3 of 4: create a maethod which will be the target method of the delegate { richTextBox1.AppendText("The target method has been calledrn"); // execution recipe 2 of 2: do stuff } private void button1_Click(object sender, System.EventArgs e) { dMydel(); // execution recipe step 1 of 2: make the delegate call the method dMydel.Invoke(); // execution recipe step 1 of 2: there are loads of ways to do the same thing in C# } } }
  • 9. Coming Soon • //TODO …… • simple delegate (synchronous) • simple delegate (semi-synchronous) • intermediate delegate (pseudo-synchronous) • intermediate delegate (ok: asynchronous) • intermediate delegate (very asynchronous, and an extra method is called at the end – that’s called a “callback” • post-intermediate ….. • what next •