From Delegates To Threads 01

50 %
50 %
Information about From Delegates To Threads 01
Technology

Published on July 12, 2009

Author: guest76869a38

Source: slideshare.net

Description

C# introduction to delegates.
Skeleton Draft part 1 of 7

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 •

Add a comment

Related presentations

Presentación que realice en el Evento Nacional de Gobierno Abierto, realizado los ...

In this presentation we will describe our experience developing with a highly dyna...

Presentation to the LITA Forum 7th November 2014 Albuquerque, NM

Un recorrido por los cambios que nos generará el wearabletech en el futuro

Um paralelo entre as novidades & mercado em Wearable Computing e Tecnologias Assis...

Microsoft finally joins the smartwatch and fitness tracker game by introducing the...

Related pages

Multithreading, Delegates, and Custom Events - CodeProject

Multithreading, Delegates, ... 01 : I was wondering if ... and the purpose of this demo was to illustrate the use of the threads, custom events and delegates.
Read more

Drag Drop, Threading, Delegates und andere Ungeheuer ...

Nachricht veröffentlicht in der 06/01/2007 ... Hallo kann mir jemand sagen wie ich dynamisch Delegates für ein Event hinzufügen ... Delegates und Threads...
Read more

Delegates and Threads - YouTube

Delegates and Threads ... Lesson 4 - Delegates Events and Lambda expressions - Duration: ... 01. Preab Sor 879 views. 31:01
Read more

Chapter 17. Delegates and Events - msdn.microsoft.com

Delegates and Events. This chapter is excerpted from Learning C# 3.0: ... Delegates with multiple subscribers are called multicast delegates.
Read more

VB.net, Invoke, delegates, and threading. Can't figure out ...

... Invoke, delegates, ... the threads don't necessary return in the ... You might also like to check out Reactive Extensions jensbits.com/2012/01/23/ ...
Read more

Delegates - C# Tutorial

Delegate-Typen definieren die Aufgabe eines Threads. ... Delegates. tolles Video: ... 01.09.2016. Dieser Online-Kurs ...
Read more

delegates, threads, timers and 'invokerequired'

delegates, threads, ... i am not familiar with delegates, ... 01:11 AM VooDoo Chicken 0. 0.
Read more

C# events and delegates from thread

C# events and delegates from thread. ... threads sends argsTypeA, ... Tuesday, November 16, 2010 2:01 PM.
Read more

【学习笔记】C# Delegates and Events - 讲的比较系统 - 类型安全的心 - 博客园

... Jeff Suddeth explains delegates and their applications in anonymous methods, asynchonous method calls, events, multicast delegates, threads, ... 01 ...
Read more