1 / 28

Writing Multithreaded Code in .NET

Writing Multithreaded Code in .NET. Fundamentals . Delegates Events. Delegates. Provide a way to bind to a method on a target object. Eliminates the need to supply a object reference at invocation time. Maintain two fields: MethodPtr

Lucy
Télécharger la présentation

Writing Multithreaded Code in .NET

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Writing Multithreaded Code in .NET Ali Aghareza Writing Multithreaded Code in .NET

  2. Fundamentals • Delegates • Events Ali Aghareza Writing Multithreaded Code in .NET

  3. Delegates • Provide a way to bind to a method on a target object. • Eliminates the need to supply a object reference at invocation time. • Maintain two fields: • MethodPtr • Target object reference (null if binding to static method) Ali Aghareza Writing Multithreaded Code in .NET

  4. Delegates Cont. • Similar to a method interface. • Differences • Interfaces require target’s method type to have pre declared compatibility with interface type. • Delegates can be bound to methods on any type. Ali Aghareza Writing Multithreaded Code in .NET

  5. Delegate Methods • CreateDelegate • Combine • Remove • Invoke • BeginInvoke/EndInvoke Ali Aghareza Writing Multithreaded Code in .NET

  6. Demo

  7. Events • Basically just a delegate • Must be defined within a class • Provide a mechanism for adding and removing delegates to a delegate chain within a class Ali Aghareza Writing Multithreaded Code in .NET

  8. Demo

  9. Threading • Overview • Threading in .Net • .Net Thread Pool Ali Aghareza Writing Multithreaded Code in .NET

  10. Threading Overview • A process has two independent concepts: • resource grouping • a thread of execution • Threads are entities scheduled for execution on the CPU Ali Aghareza Writing Multithreaded Code in .NET

  11. Threading Overview Cont. • Threads keep track what should be executed, and what it has already executed • Every process has at least one thread • Multithreading • Having multiple threads within a process Ali Aghareza Writing Multithreaded Code in .NET

  12. Thread State Model Running Blocked Ready Ali Aghareza Writing Multithreaded Code in .NET

  13. Why have multiple threads • Advantage • Allows the user interface to remain responsive, while running I/O tasks in the background • Disadvantage • Having many threads within a process can be complex. Ali Aghareza Writing Multithreaded Code in .NET

  14. .Net Thread Methods • Start • Sleep • Interrupt • Resume/Suspend – Do not use • Should not be considered for a synchronization mechanisms Ali Aghareza Writing Multithreaded Code in .NET

  15. Safe Points • A point in execution at which garbage collection can be performed. • A thread will not suspend until it reaches a safe point. • When the CLR decided to GC, it suspends all threads except for the thread that is going to do the collection. Ali Aghareza Writing Multithreaded Code in .NET

  16. Thread Safety Examples Ali Aghareza Writing Multithreaded Code in .NET

  17. Thread Safety • Operations that can be performed from multiple threads safely, even if the calls happen simultaneously on multiple threads is said to be “Thread Safe” • All collections except Hashtable are not “thread safe” Ali Aghareza Writing Multithreaded Code in .NET

  18. Demo

  19. Writing Multithreaded WinForms • Window Form controls are not thread safe. • Thread Safe Methods • Invoke / BeginInvoke / EndInvoke • InvokeRequired Ali Aghareza Writing Multithreaded Code in .NET

  20. Demo

  21. Foreground/Background Threads • A background thread will not keep an application alive. • A background thread executes only when the main user thread is idle. • A foreground thread starves background threads of processor time. • Thread.IsBackground is by default false Ali Aghareza Writing Multithreaded Code in .NET

  22. Demo

  23. ThreadPool • Provides the ability to call a method asynchronously • Provides the ability to call a method at timed intervals • Provides the ability to call a method when an asynchronous I/O request completes Ali Aghareza Writing Multithreaded Code in .NET

  24. ThreadPool Cont. • Delegate • BeginInvoke/EndInvoke • Timer • Uses the ThreadPool to call methods at timed intervals • WebService BeginXxx/EndXxx method calls Ali Aghareza Writing Multithreaded Code in .NET

  25. Demo

  26. Demo

  27. Cool Tools • Resharper • www.jetbrains.com • Process Explorer • www.sysinternals.com Ali Aghareza Writing Multithreaded Code in .NET

  28. Resources • “Applied .NET Programming” Jeff Richter • “Essential .NET” Don Box • “Essential WinForms” Chris Sells • www.msdn.com Ali Aghareza Writing Multithreaded Code in .NET

More Related