280 likes | 388 Views
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
E N D
Writing Multithreaded Code in .NET Ali Aghareza Writing Multithreaded Code in .NET
Fundamentals • Delegates • Events Ali Aghareza Writing Multithreaded Code in .NET
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
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
Delegate Methods • CreateDelegate • Combine • Remove • Invoke • BeginInvoke/EndInvoke Ali Aghareza Writing Multithreaded Code in .NET
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
Threading • Overview • Threading in .Net • .Net Thread Pool Ali Aghareza Writing Multithreaded Code in .NET
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
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
Thread State Model Running Blocked Ready Ali Aghareza Writing Multithreaded Code in .NET
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
.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
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
Thread Safety Examples Ali Aghareza Writing Multithreaded Code in .NET
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
Writing Multithreaded WinForms • Window Form controls are not thread safe. • Thread Safe Methods • Invoke / BeginInvoke / EndInvoke • InvokeRequired Ali Aghareza Writing Multithreaded Code in .NET
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
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
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
Cool Tools • Resharper • www.jetbrains.com • Process Explorer • www.sysinternals.com Ali Aghareza Writing Multithreaded Code in .NET
Resources • “Applied .NET Programming” Jeff Richter • “Essential .NET” Don Box • “Essential WinForms” Chris Sells • www.msdn.com Ali Aghareza Writing Multithreaded Code in .NET