SlideShare une entreprise Scribd logo
1  sur  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
•

Contenu connexe

Dernier

Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Farhan Tariq
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsNathaniel Shimoni
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)Kaya Weers
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfIngrid Airi González
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality AssuranceInflectra
 
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesMuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesManik S Magar
 
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical InfrastructureVarsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructureitnewsafrica
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationKnoldus Inc.
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Alkin Tezuysal
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS:  6 Ways to Automate Your Data IntegrationBridging Between CAD & GIS:  6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integrationmarketing932765
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Mark Goldstein
 

Dernier (20)

Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directions
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdf
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
 
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesMuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
 
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical InfrastructureVarsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog Presentation
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS:  6 Ways to Automate Your Data IntegrationBridging Between CAD & GIS:  6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
 

En vedette

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
 

En vedette (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 •