Presentation is loading. Please wait.

Presentation is loading. Please wait.

1.NETDelegates & eventsNOEA / PQC 2007 Delegates & events Observer pattern Delegates –Semantics –Cil – code –Usage Events Asynchronious delegates.

Similar presentations


Presentation on theme: "1.NETDelegates & eventsNOEA / PQC 2007 Delegates & events Observer pattern Delegates –Semantics –Cil – code –Usage Events Asynchronious delegates."— Presentation transcript:

1 1.NETDelegates & eventsNOEA / PQC 2007 Delegates & events Observer pattern Delegates –Semantics –Cil – code –Usage Events Asynchronious delegates

2 2.NETDelegates & eventsNOEA / PQC 2007 An example (from java!) public class FrameMedKnap extends javax.swing.JFrame { private javax.swing.JButton jButton1; public FrameMedKnap() { jButton1 = new javax.swing.JButton(); jButton1.setText("jButton1"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); this.add(jButton1, java.awt.BorderLayout.CENTER); } private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { jButton1.setText("Der er trykket på knap"); }

3 3.NETDelegates & eventsNOEA / PQC 2007 Observer pattern Also called Observable in some litterature Might also be a interface

4 4.NETDelegates & eventsNOEA / PQC 2007 Callback interfaces Interfaces describes a common behavior that is shared by different classes Used in Observer pattern to construct a callback mecanism –The key is the Update() method in the Observer interface. –Note that it do not have to be named Update ;-) Callback can be used in languages that supports interfaces Callback can be implemented with function pointers in C (But then it is not by using observer pattern)

5 5.NETDelegates & eventsNOEA / PQC 2007 Subject (or Observable) implements an add and a remove method to add and remove observers to/from an internal container (typical an arraylist) Has also a notify() method that calls update() on all observer object in the container Observer implements the update() method. That will be the event handler in an event based system What is what in the java example?

6 6.NETDelegates & eventsNOEA / PQC 2007 Delegate Delegate is a new language construction in C# Java programmers will compare it to observer pattern C/C++, Pascal/Delphi… programmers will compare it to function pointers A delegate gives the possibility to pass a method of a class as a parameter to objects of another class. The method can be a static or an instance method Delegates are object oriented, typesafe and undependent of method name. Delegates are objects itselves of classes that iniherit from System.Delegate via System.MulticastDelegate

7 7.NETDelegates & eventsNOEA / PQC 2007

8 8 4 steps to create and use delegates 1.Declare a delegate with a appropiate signature 2.Define method(s) with the same signature 3.Instantiate a delegate object with a method as parameter The method will be encapsulated in the delegate 4.Call the method by the delegate

9 9.NETDelegates & eventsNOEA / PQC 2007 Step 1: 1.Declare a delegate with a appropiate signature. That means with the same parameters and return values as the method to be encapsulated delegate void Notifier(string sender);

10 10.NETDelegates & eventsNOEA / PQC 2007 Step 2: Define method with the same signature as the delegate void SayHello (string sender) { Console.WriteLine("Hello from "+ sender); } void SayGoodBye (string sender) { Console.WriteLine("Good Bye from "+ sender); }

11 11.NETDelegates & eventsNOEA / PQC 2007 Step 3: Instantiate the delegate object and add the methods Notifier notify = new Notifier(SayHello); //Instantiate delegate and thereby a container notify += new Notifier(SayGoodBye); //Add a new Notifier to the container (multicast); //For the example: remove Notifier: notify -= new Notifier(SayHello);

12 12.NETDelegates & eventsNOEA / PQC 2007 Trin 4: Call the methods via the delegate: notify ("Pingo");

13 13.NETDelegates & eventsNOEA / PQC 2007 Break opgave Implement a Watch class The time must be updated every second –Use Thread.Sleep(1000) or Thread.CurrentThread.Join(1000) for suspending the program in one second –The time may be updated with _currentTime =_currentTime.AddSeconds(1f); eller _currentTime=DateTime.Now; It must be possible to be notified when the time is updated. Make console application that continuously shows the time

14 14.NETDelegates & eventsNOEA / PQC 2007 Parallel programming Asynchronous delegates –BeginInvoke and EndInvoke –CallBack

15 15.NETDelegates & eventsNOEA / PQC 2007 WaitingClass (used as example for delegates) class WaitingClass { private double Wait(int id, int secs) { DateTime t1, t2; TimeSpan dt; Console.WriteLine("Wait ({0}): The time is {1:mm.ss}. Waits for {2} secs", id, t1 = DateTime.Now, secs); Thread.Sleep(1000 * secs); t2 = DateTime.Now; dt = t2 - t1; Console.WriteLine("Wait ({0}): The time {1:mm.ss}. Waited for {2:0.0000} secs", id, t2, dt.TotalSeconds); return dt.TotalSeconds; } continue....

16 16.NETDelegates & eventsNOEA / PQC 2007 WaitingClass continued public double Wait2(int secs) { Console.WriteLine("Wait2 runs in thread: {0}", Thread.CurrentThread.GetHashCode()); secs = 2 * secs; return Wait(2, secs); } public double Wait1(int secs) { Console.WriteLine("Wait1 runs in thread: {0}", Thread.CurrentThread.GetHashCode()); return Wait(1, secs); }

17 17.NETDelegates & eventsNOEA / PQC 2007 The WaitDelegate Here: Declared outside the class public delegate double WaitDelegate(int secs);

18 18.NETDelegates & eventsNOEA / PQC 2007 Synchronious delegate Console.WriteLine("Synchronious delegate"); WaitingClass v = new WaitingClass(); WaitDelegate WaitDelegate = new WaitDelegate(v.Wait1); WaitDelegate += new WaitDelegate(v.Wait2); DateTime t1 = DateTime.Now; WaitDelegate(2); DateTime t2 = DateTime.Now; Console.WriteLine("Time was {0}", ((TimeSpan)(t2 - t1)).TotalSeconds); Thread.Sleep is not precise

19 19.NETDelegates & eventsNOEA / PQC 2007 Synchronious delegate The methods are executed in the same thread The total execution time is the sum of the execution time for each method. Thread.CurrentThread gives a reference to the active thread myThread.GetHashCode() gives an id for the thread Thread.Sleep(int milliSecs) puts the thread in sleep state for a number of milli seconds. But it depends on the OS' thread controlling

20 20.NETDelegates & eventsNOEA / PQC 2007 Asynchronious delegate A asynchronious delegate starts with BeginInvoke(....) BeginInvoke's signature is: IAsyncResult BeginInvoke(delegate signatur, AsyncCallback, Object) Get the return value with EndInvoke. EndInvoke's signature is: delegate retur EndInvoke(evt. delegate ref and out, IAsyncResult) BeginInvoke and EndInvoke are 'implemented' at compile time. They are badly documented and depends on the compiler But asynchronious delegates are a easy and pretty safe way to start a thread.

21 21.NETDelegates & eventsNOEA / PQC 2007 Example 1: BeginInvoke and EndInvoke Console.WriteLine("\nASynchronios delegate 1: EndInvoke without condition"); WaitingClass v = new WaitingClass(); WaitDelegate waitDelegate1 = new WaitDelegate(v.Wait1); WaitDelegate waitDelegate2 = new WaitDelegate(v.Wait2); DateTime t1 = DateTime.Now; IAsyncResult iaResult1 = waitDelegate1.BeginInvoke(2, null, null); IAsyncResult iaResult2 = waitDelegate2.BeginInvoke(2, null, null); double vt1 = waitDelegate1.EndInvoke(iaResult1); double vt2 = waitDelegate2.EndInvoke(iaResult2); DateTime t2 = DateTime.Now; Console.WriteLine("Wait1 waited {0} secs, Wait2 waited {1} secs. Total time was {2}", vt1, vt2, ((TimeSpan)(t2 - t1)).TotalSeconds); EndInvoke blocks Main's thread

22 22.NETDelegates & eventsNOEA / PQC 2007 Example 2: IsComplete - avoid that EndInvoke blocks... DateTime t1 = DateTime.Now; IAsyncResult iaResult1 = waitDelegate1.BeginInvoke(2, null, null); IAsyncResult iaResult2 = waitDelegate2.BeginInvoke(2, null, null); while (!iaResult1.IsCompleted && !iaResult2.IsCompleted) { Console.Write("."); Thread.Sleep(100); } Console.WriteLine(); double vt1 = waitDelegate1.EndInvoke(iaResult1); double vt2 = waitDelegate2.EndInvoke(iaResult2); DateTime t2 = DateTime.Now; Console.WriteLine("Wait1 waited {0} secs, Wait2 waited {1} secs. Total time was {2}", vt1, vt2, ((TimeSpan)(t2 - t1)).TotalSeconds);

23 23.NETDelegates & eventsNOEA / PQC 2007 Result Working

24 24.NETDelegates & eventsNOEA / PQC 2007 Example 3: With WaitOne Makes it possible to handle timeout Console.WriteLine("\nASynchronios delegate 3: EndInvoke with WaitOne condition"); DateTime t1 = DateTime.Now; IAsyncResult iaResult1 = waitDelegate1.BeginInvoke(2, null, null); IAsyncResult iaResult2 = waitDelegate2.BeginInvoke(2, null, null); while (!iaResult1.AsyncWaitHandle.WaitOne(100, true) && !iaResult2.AsyncWaitHandle.WaitOne(100, true)) { Console.Write("."); } double vt1 = waitDelegate1.EndInvoke(iaResult1); double vt2 = waitDelegate2.EndInvoke(iaResult2); DateTime t2 = DateTime.Now; Waits 100 milli seconds for each WaitOne If the method are finish within the time, true is returned else false Note that boolean expression in C# are 'shorting out', meaning that the first part of an AND is false, then the second is not checked. Similary is the second part of OR not checked if the first part is true

25 25.NETDelegates & eventsNOEA / PQC 2007 Example 4: Callback Avoid the loop.... WaitingClass v = new WaitingClass(); WaitDelegate waitDelegate1 = new WaitDelegate(v.Wait1); WaitDelegate waitDelegate2 = new WaitDelegate(v.Wait2); DateTime t1 = DateTime.Now; IAsyncResult iaResult1 = waitDelegate1.BeginInvoke(2, v.WaitComplete, t1); IAsyncResult iaResult2 = waitDelegate2.BeginInvoke(2, v.WaitComplete, t1); DateTime t2 = DateTime.Now; public void WaitComplete(IAsyncResult IAResult) { Console.WriteLine("WaitComplete runs in thread {0}", Thread.CurrentThread.GetHashCode()); Console.WriteLine("WaitComplete: Waited for {0:0.0000} secs", ((TimeSpan)(DateTime.Now - (DateTime)IAResult.AsyncState)).TotalSeconds); System.Runtime.Remoting.Messaging.AsyncResult ar=(System.Runtime.Remoting.Messaging.AsyncResult) IAResult; WaitDelegate ventDelegate =(WaitDelegate) ar.AsyncDelegate; Console.WriteLine("Return value is: "+ventDelegate.EndInvoke(IAResult).ToString()); } Callback method Status object Here: time before Get the status object An IAsyncResult object shall be passed Get the delegate Call EndInvoke to get a return value

26 26.NETDelegates & eventsNOEA / PQC 2007 Result:


Download ppt "1.NETDelegates & eventsNOEA / PQC 2007 Delegates & events Observer pattern Delegates –Semantics –Cil – code –Usage Events Asynchronious delegates."

Similar presentations


Ads by Google