- Operating System manages processes, while .NET Runtime manages Application Domains.
- If we consider an Assembly as class, then its instances could be called Application Domains.
- If an Application uses more than one assembly, it is possible to create a seperate process for each of them. The problem would be the performance hit ad resource usage caused between context shifts from one process to another. Therefore it is better to use one Assembly that acts as a host for other Assemblies. Then we may share resources (data) among Assemblies within an Application Domain. (e.g. ASP.NET Application object is an Application Domain)
- Points-to-Ponder: (See page 439)
- AppDomain class allows for creating Application Domains within .NET
- AppDomain.CreateDomain( ) static method is used to create a new Application Domain with a friendly name, and RunAssembly( ) instance-level method is used to load an Assembly within a domain.
Saturday, July 18, 2009
Chapter 8-Lesson 1
Wednesday, July 15, 2009
Chapter 7-Lesson 3
- Asynchronous Programming Model allows us increase performance of our application by running certain piece of code onto a seperate thread while the main thread is blocked for some reason.
- .NET supports APM by supplying BeginXXX and EndXXX methods built-in to various library classes. (e.g. BeginRead( ) and EndRead( ) methods in Stream class)
- Rendezvous Models allow for three styles of using APM, Wait-Until-Done, Polling and Callback.
- Use null while calling an APM supportive method for Callback and state arguments to use Wait-Until-Done. If we use this, EndXXX will block current thread until Asynchronous call is completed.
- The Completed property of the IAsyncResult object returned by the BeginXXX method may be used to check (poll) whether the Asynchronous work has been completed. If not, we may keep on working on current thread.
- If BeginXXX is called by one method and EndXXX needs to be called by another method, we would need Callback Model.
- Callback model allows us to specify a method (using AsyncCallback delegate) in the BeginXXX method. This method would be called in order to complete the Asynchronous call (EndXXX).
- We may optionally supply an object (as a state object) that might be needed to call EndXXX. (Refer to page 417)
- If an Exception occurs during Asynchronous method calls, it is thrown when EndXXX is called. Reason: Since it occurred on the other thread, we cannot catch it on the main thread.
- However, in Windows Forms Applications, the Application object is notified whenever an exception occurs (either on main thread and worker threads). Therefore, we can attach a method to Application.ThreadException to handle an exceptional situation.
- Creating threads on our own is not recommended. Instead use ThreadPool class to create/reuse threads for our methods. (Don't buy threads, lease 'em)
- ThreadPool has a number of available slots to be used as threads. These slots are maintained by ThreadPool class and it keeps processor busy by continuously assigning to it slots that have pending work in them. These reusable slots may be called pre-built threads.
- We make a request to acquire a slot (whenever one becomes available) by calling ThreadPool.QueueUserWorkItem( ) static method and pass it a WaitCallback delegate representing the name of the method we wish to run as thread.
- If the Application uses a large number of Threads through ThreadPool, Threads may start to starve. In such a situation we may set the SetMaxThreads property of ThreadPool class to incease number of pre-built threads (slots).
- If faster Startup time is required, we may set the minimum number of threads in ThreadPool using SetMinThreads property.
- Typically ThreadPool class limits minimum number of threads in the pool to be 2 per second.
Labels:
APM,
Asynchronous,
Blocked,
Polling,
Rendezvous,
Starvation,
ThreadPool
Tuesday, July 14, 2009
Chapter 7-Lesson 2
- There are three operations usually involved with arithmetic operations on variables, i.e. Load Variable value into Register, Change value in Register, Write Back the new value to variable.
- In threaded code, all the above steps/operations are treated as atomic operations.
- Therefore, it might happen that two threads read/load the same variable into the register, change its value equally and write it back, all at the same time. (Valid in multi-core or multi-processor scenario)
- Interlocked class may be used to accertain that a certain variable is accessed by only one thread in a multi-threaded application.
- Interlocked class has static methods which can be used when performing arithmetic operations on variables.
- However, interlocked class has some limitations. It cannot be used for a wide variety of types and cannot be used to make pieces of code thread-safe. (Refer to Page 392)
- To declare a piece of code as thread-safe, use lock keyword to create thread-safe block. Lock keyword requires and object reference passed to it (usually this).
- This creation of lock block is called Synchronization Locks.
- Synchronization Locks may also be created using Monitor class static methods.
- To avoid deadlocks, we may use Monitor.TryEnter( ) method. Otherwise use Timeout.Infinite static property to tell the thread to wait as long as it takes.
- If we wish to deal (synchronize) reading and writing operations seperately, we may use ReaderWriterLock class to lock certain data only for reading or writing.
- LockCookie type object would be required to downgrade a WriterLock to a ReaderLock. (This implies that a WriterLock is downgradable only if it was previously upgraded from a ReaderLock)
- Three OS kernel level objects are represented in .NET by classes named Mutex, Semaphore, AutoResetEvent and ManualResetEvent. All of these derive from WaitHandle class.
- Mutex is almost same as Monitor class with the fact that it allows synchronization across AppDomain and process boundaries.
Labels:
Interlocking,
Lock,
Synchronization,
Thread,
Thread Safety
Chapter 7-Lesson 1
- Thread class is used to create and start a thread.
- ManagedThreadId property may not be the same as operating system thread ID.
- Abort( ) methods actually raises the ThreadAbortException to kill the thread.
- Resume( ) and Suspend( ) methods have been deprecated.
- In order to notify the thread host about the critical region of a thread, use static methods BeginCriticalRegion( ) and EndCriticalRegion( ) of Thread class. Use these methods inside a method that would be used as a thread.
- The static Sleep( ) method blocks the current thread for a certain amount of time during which other threads may run.
- SpinWait( ) does the same but does not allow other thread to run during that time.
- Join( ) method of Thread class asks the host thread to wait until the child/worker thread has finished executing.
- ParameterizedThreadStart delegate is used to represent threads that require parameters/arguments for calling.
- Page 383*
Friday, July 10, 2009
Chapter 6-Lesson 3
- Font class along with Graphics.DrawString( ) method may be used to draw text on forms, Bitmaps and Images.
- StringAlignment.Near actually aligns text to Left.
- Font class has 13 constructor overloads.
- LineAlignment flag determines vertical alignment of text.
Thursday, July 9, 2009
Chapter 6-Lesson 2
- Image is an abstract class.
- Image class uses static methods to create objects to/from image files.
- Bitmap class inherits from Image class and is used for still images.
- MetaFile class also inherits from Image class and is used for Animated images.
- Graphics class is still more complex than Bitmap class. We can always use Graphics.FromImage( ) method to represent a Bitmap as a Graphics object and use Graphics methods over it.
- To save images to a file, use Bitmap.Save( ) method.
- ImageFormat enum specifies supported image formats (BMP, GIF, JPEG, TIFF, etc) for saving images.
- SystemIcons class exposes standard windows icons (of size 40 x 40) as properties.
Sunday, July 5, 2009
Chapter 6-Lesson 1
- Brush is an abstract class. Brushes is a sealed class.
- ColorConverter and ColorTranslator are used to convert colour value from one format to another.
- Graphics is a sealed class.
- Pen (and related) classes are used to draw shapes and Brush (and related classes) are used to fill regions with colours.
- All classes suffixed with Converter are accessed through TypeDescriptor.
- Color, Rectangle, Point and Size are structures and not classes.
- Color names in Color structure are actually properties inside the structure.
- DrawString( ) method of the Graphics class may be used to draw a string using a specified Brush and Font objects.
- By default Pen class draws solid lines
- Usually System.Drawing.Drawing2D namespace is required for Graphics programming.
- LineCap enumeration defines possible values to set edges of a line.
- Draw methods usuallu require a Pen type object and Fill methods usually require a Brush type object.
- Brush is an abstract class. Use one the childrens instead.
Subscribe to:
Posts (Atom)