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
•