SlideShare ist ein Scribd-Unternehmen logo
1 von 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
•

Weitere ähnliche Inhalte

Kürzlich hochgeladen

Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Orbitshub
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamUiPathCommunity
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...apidays
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Victor Rentea
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Zilliz
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Victor Rentea
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...apidays
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdfSandro Moreira
 
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
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024The Digital Insurer
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfOrbitshub
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 

Kürzlich hochgeladen (20)

Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
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
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 

Empfohlen

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
 

Empfohlen (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 •