Understanding Delegates and Event Handling in Smart Home Automation Systems

Slide Note
Embed
Share

Exploring the concepts of delegates and event handling in the context of smart home automation systems. Delegates serve as object-oriented function pointers, while event handling allows independent components to communicate effectively. Delegates can hold and invoke multiple methods, enabling efficient event management within the system.


Uploaded on May 15, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. CMSC 691: Systems for Smart Home Automation Nilanjan Banerjee University of Maryland Baltimore County nilanb@umbc.edu http://www.csee.umbc.edu/~nilanb/teaching/691/ Smart Home Automation 1

  2. Todays Lecture Advanced Concepts in C# 2

  3. Delegates Overview A delegate is a reference type that defines a method signature A delegate instance holds one or more methods Essentially an object-oriented function pointer Methods can be static or non-static Methods can return a value Foundation for event handling

  4. Delegates Overview delegate double Del(double x); // Declare static void DemoDelegates() { Del delInst = new Del(Math.Sin); // Instantiate double x = delInst(1.0); // Invoke }

  5. Delegates Multicast Delegates A delegate can hold and invoke multiple methods Multicast delegates must contain only methods that return void, else there is a run-time exception Each delegate has an invocation list Methods are invoked sequentially, in the order added The += and -= operators are used to add and remove delegates, respectively += and -= operators are thread-safe

  6. Delegates Multicast Delegates delegate void SomeEvent(int x, int y); static void Foo1(int x, int y) { Console.WriteLine("Foo1"); } static void Foo2(int x, int y) { Console.WriteLine("Foo2"); } public static void Main() { SomeEvent func = new SomeEvent(Foo1); func += new SomeEvent(Foo2); func(1,2); // Foo1 and Foo2 are called func -= new SomeEvent(Foo1); func(2,3); // Only Foo2 is called }

  7. Delegates and Interfaces Could always use interfaces instead of delegates Interfaces are more powerful Multiple methods Inheritance Delegates are more elegant for event handlers Less code Can easily implement multiple event handlers on one class/struct

  8. Events Overview Event handling is a style of programming where one object notifies another that something of interest has occurred A publish-subscribe programming model Events allow you to tie your own code into the functioning of an independently created component Events are a type of callback mechanism

  9. Events Overview Events are well suited for user-interfaces The user does something (clicks a button, moves a mouse, changes a value, etc.) and the program reacts in response Many other uses, e.g. Time-based events Asynchronous operation completed Email message has arrived A web session has begun

  10. Events Overview C# has native support for events Based upon delegates An event is essentially a field holding a delegate However, public users of the class can only register delegates They can only call += and -= They can t invoke the event s delegate Multicast delegates allow multiple objects to register with the same event

  11. Events Example: Component-Side Define the event signature as a delegate public delegate void EventHandler(object sender, EventArgs e); Define the event and firing logic public class Button { public event EventHandler Click; protected void OnClick(EventArgs e) { // This is called when button is clicked if (Click != null) Click(this, e); } }

  12. Events Example: User-Side Define and register an event handler public class MyForm: Form { Button okButton; static void OkClicked(object sender, EventArgs e) { ShowMessage("You pressed the OK button"); } public MyForm() { okButton = new Button(...); okButton.Caption = "OK"; okButton.Click += new EventHandler(OkClicked); } }

  13. Attributes Overview It s often necessary to associate information (metadata) with types and members, e.g. Documentation URL for a class Transaction context for a method XML persistence mapping COM ProgID for a class Attributes allow you to decorate a code element (assembly, module, type, member, return value and parameter) with additional information

  14. Attributes Overview [HelpUrl( http://SomeUrl/APIDocs/SomeClass )] class SomeClass { [Obsolete( Use SomeNewMethod instead )] public void SomeOldMethod() { ... } public string Test([SomeAttr()] string param1) { ... } }

  15. Attributes Overview Attributes are superior to the alternatives Modifying the source language Using external files, e.g., .IDL, .DEF Attributes are extensible Attributes allow to you add information not supported by C# itself Not limited to predefined information Built into the .NET Framework, so they work across all .NET languages Stored in assembly metadata

  16. Attributes Overview Some predefined .NET Framework attributes Attribute Name Description Should a property or event be displayed in the property window Browsable Allows a class or struct to be serialized Serializable Compiler will complain if target is used Obsolete COM Prog ID ProgId Transactional characteristics of a class Transaction

  17. Attributes Overview Attributes can be Attached to types and members Examined at run-time using reflection Completely extensible Simply a class that inherits from System.Attribute Type-safe Arguments checked at compile-time Extensive use in .NET Framework XML, Web Services, security, serialization, component model, COM and P/Invoke interop, code configuration

  18. Attributes Querying Attributes [HelpUrl("http://SomeUrl/MyClass")] class Class1 {} [HelpUrl("http://SomeUrl/MyClass"), HelpUrl("http://SomeUrl/MyClass , Tag= ctor )] class Class2 {} Type type = typeof(MyClass); foreach (object attr in type.GetCustomAttributes() ) { if ( attr is HelpUrlAttribute ) { HelpUrlAttribute ha = (HelpUrlAttribute) attr; myBrowser.Navigate( ha.Url ); } }

  19. Preprocessor Directives Overview C# provides preprocessor directives that serve a number of functions Unlike C++, there is not a separate preprocessor The preprocessor name is preserved only for consistency with C++ C++ preprocessor features removed include: #include: Not really needed with one-stop programming; removal results in faster compilation Macro version of #define: removed for clarity

  20. Preprocessor Directives Overview Directive Description #define, #undef Define and undefine conditional symbols #if,#elif, #else, #endif Conditionally skip sections of code #error, #warning Issue errors and warnings #region, #end Delimit outline regions Specify line number #line

  21. Preprocessor Directives Conditional Compilation #define Debug public class Debug { [Conditional("Debug")] public static void Assert(bool cond, String s) { if (!cond) { throw new AssertionException(s); } } void DoSomething() { ... // If Debug is not defined, the next line is // not even called Assert((x == y), X should equal Y ); ... } }

  22. Preprocessor Directives Assertions By the way, assertions are an incredible way to improve the quality of your code An assertion is essentially a unit test built right into your code You should have assertions to test preconditions, postconditions and invariants Assertions are only enabled in debug builds Your code is QA d every time it runs

  23. XML Comments Overview Programmers don t like to document code, so we need a way to make it easy for them to produce quality, up-to-date documentation C# lets you embed XML comments that document types, members, parameters, etc. Denoted with triple slash: /// XML document is generated when code is compiled with /doc argument Comes with predefined XML schema, but you can add your own tags too Some are verified, e.g. parameters, exceptions, types

  24. XML Comments Overview XML Tag Description Type or member <summary>, <remarks> <summary>, <remarks> Method parameter <param> <param> Method return value <returns> <returns> Exceptions thrown from method <exception> <exception> Sample code <example>, <c>, <code> <example>, <c>, <code> Cross references <see>, <seealso> <see>, <seealso> Property <value> <value> Use of a parameter <paramref> <paramref> Formatting hints <list>, <item>, ... <list>, <item>, ... Permission requirements <permission> <permission>

  25. XML Comments Overview class XmlElement { /// <summary> /// Returns the attribute with the given name and /// namespace</summary> /// <param name="name"> /// The name of the attribute</param> /// <param name="ns"> /// The namespace of the attribute, or null if /// the attribute has no namespace</param> /// <return> /// The attribute value, or null if the attribute /// does not exist</return> /// <seealso cref="GetAttr(string)"/> /// public string GetAttr(string name, string ns) { ... } }

  26. Unsafe Code Overview Developers sometime need total control Performance extremes Dealing with existing binary structures Existing code Advanced COM support, DLL import C# allows you to mark code as unsafe, allowing Pointer types, pointer arithmetic ->, * operators Unsafe casts No garbage collection

  27. Unsafe Code Overview Lets you embed native C/C++ code Basically inline C Must ensure the GC doesn t move your data Use fixed statement to pin data Use stackalloc operator so memory is allocated on stack, and need not be pinned unsafe void Foo() { char* buf = stackalloc char[256]; for (char* p = buf; p < buf + 256; p++) *p = 0; ... }

  28. Unsafe Code Overview class FileStream: Stream { int handle; public unsafe int Read(byte[] buffer, int index, int count) { int n = 0; fixed (byte* p = buffer) { ReadFile(handle, p + index, count, &n, null); } return n; } [dllimport("kernel32", SetLastError=true)] static extern unsafe bool ReadFile(int hFile, void* lpBuffer, int nBytesToRead, int* nBytesRead, Overlapped* lpOverlapped); }

  29. Unsafe Code C# and Pointers Power comes at a price! Unsafe means unverifiable code Stricter security requirements Before the code can run Downloading code

  30. Threading using C# and .Net Threads : Thread is the fundamental unit of execution. More than one thread can be executing code inside the same process (application). On a single-processor machine, the operating system is switching rapidly between the threads, giving the appearance of simultaneous execution.

  31. With threads you can : Maintain a responsive user interface while background tasks are executing Distinguish tasks of varying priority Perform operations that consume a large amount of time without stopping the rest of the application

  32. System.Threading Namespace Provides classes and interfaces that enable multithreaded programming. Consists of classes for synchronizing thread activities . Chief among the namespace members is Thread class

  33. Thread Class - Implements various methods & properties that allows to manipulate concurrently running threads. - Some of them are : CurrentThread IsAlive IsBackground Name Priority ThreadState

  34. Starting a thread : Thread thread = new Thread(new ThreadStart (ThreadFunc)); //Creates a thread object // ThreadStart identifies the method that the thread executes when it //starts thread.Start(); //starts the thread running Thread Priorities : Controls the amount of CPU time that can be allotted to a thread. ThreadPriority.Highest ThreadPriority.AboveNormal ThreadPriority.Normal ThreadPriority.BelowNormal ThreadPriority.Lowest

  35. Suspending and Resuming Threads Thread.Suspend temporarily suspends a running thread. Thread.Resume will get it running again Sleep : A thread can suspend itself by calling Sleep. Difference between Sleep and Suspend - A thread can call sleep only on itself. -Any thread can call Suspend on another thread.

  36. Terminating a thread Thread.Abort() terminates a running thread. In order to end the thread , Abort() throws a ThreadAbortException. Suppose a thread using SQL Connection ends prematurely , we can close the the SQL connection by placing it in the finally block. - SqlConnection conn try{ conn.open(); . ..... } finally{ conn.close();//this gets executed first before the thread ends. }

  37. A thread can prevent itself from being terminated with Thread.ResetAbort. - try{ } catch(ThreadAbortException){ Thread.ResetAbort(); } Thread.Join() When one thread terminates another, wait for the other thread to end.

  38. Thread Synchronization : Threads must be coordinated to prevent data corruption. Monitors Monitors allow us to obtain a lock on a particular object and use that lock to restrict access to critical section of code. While a thread owns a lock for an object, no other thread can acquire that lock. Monitor.Enter(object) claims the lock but blocks if another thread already owns it. Monitor.Exit(object) releases the lock.

  39. Void Method1() { . Monitor.Enter(buffer); try { } finally { } } critical section; Monitor.Exit(buffer); Calls to Exit are enclosed in finally blocks to ensure that they re executed even when an exception arises.

  40. The C # Lock Keyword : lock(buffer){ . } is equivalent to - Makes the code concise. - Also ensures the presence of a finally block to make sure the lock is released. Monitor.Enter(buffer); try { critical section; } finally { Monitor.Exit(buffer); }

  41. MethodImpl Attribute For synchronizing access to entire methods. To prevent a method from be executed by more than one thread at a time , [MehtodImpl] (MethodImplOptions.Synchronized)] Byte[] TransformData(byte[] buffer) { } Only one thread at a time can enter the method.

  42. Next Lecture Primer into HomeOS (basis of Lab-of-Things) 42

Related


More Related Content