Knowledge Base, they said. Bah! It's just a hole where we dump all ideas in and bury them!

Wednesday, December 21, 2005

Handling Generic Exceptions in Windows Forms

I have had the opportunity to face interesting and curious exceptions which seems to act like a mouse evading all attempts at capture. In addition, I have recently been dabbling more into the integration aspect of software development meaning I need to learn and anticipate user interaction with the system.

One of the things which makes an application great is fantastic robustness and automatic help. For example, how would you feel if you Windows stops working everytime a small bug or error happen for instance when you click on the wrong file to open? Isn't great how it just keeps going and going and going and.... you get the picture. (Ahh... of course I'm referring only to Windows XP, not the previous version ;-) ). What I use to do, what I learnt before is to selectively add in Try ... Catch ex as Exception in parts of the code at the top most level to handle any possible unhandled exceptions and display message to user to inform them.

An alternative which I learnt about was to use a Shared Sub Main() as the entry point to the application, call Application.Run(MainWinFormOfApplicationObject) and put within a Try ... Catch ex As Exception block. However this still does not catch some exceptions which might occur.

Now there is a better way. The aforementioned methods can work sometimes but as mention above, you will still get unhandled exceptions coming seemingly from nowhere. There is a static/shared event under the Application namespace (Application.ThreadException) which allows you to handle all exceptions coming from the application. All you need is to create an event handler and wire up to this event.

The reason behind all this is because in Windows Forms, all the UI aspects e.g. close button ('X'), minimize/maximize buttons, resizing window e.t.c. are run on a separate thread from the application itself. Therefore when the UI throws an exception, it is throwing on it's own thread and the Try ... Catch ex As Exception around Application.Run() will not catch anything since there was no exception thrown within anyway!

A couple of good places to start learning this with:

Managing Unhandled Exceptions in dotNET ~ CodeProject.com
Unhandled Exception Handler ~ GeeksWithBlogs.net

However do be careful and take note that not everything works
C# IAQ #6: Why Can't I Throw An Exception in GUI Thread and Let Application.ThreadException Handle It? ~ GeeksWithBlogs.net

I'm still pretty new with this and due to other constraints am unable to pursue knowledge of this further. I will most definitely have a more detailed look at this very very interesting aspect of Windows Forms programming. Console and Web programming (ASP.NET) has their own versions of Application.ThreadException and different usage patterns.

Monday, October 03, 2005

Evaluate only when necessary

IIf is bad.

This function is a legacy from pre-dotNET Visual Basic to provide a shortcut for evaluating two expressions and returning a result. In the current Visual Basic incarnation, this functionality is still supported mainly for backward compatibility and is located in Microsoft.VisualBasic namespace.

It has been recommended by Microsofties not to use Microsoft.VisualBasic if possible as it will degrade performance by as much as 1000 times.

Unfortunately, only the first paragraph is true. The other two paragraphs are debatable. I use to think that Microsoft.VisualBasic is bad and will degrade performance. I remember reading it somewhere in some article on optimization. However when I went in search of such a thing to link from here, I discovered something new. Apparently, Microsoft.VisualBasic classes and methods are written entirely in dotNET and they are NOT performance degrading. What we are recommended NOT to use is Microsoft.VisualBasic.Compatibility.dll. How embarassing for me.

"The functions in the Microsoft.VisualBasic namespace are built directly on top of the System namespace and provide functionality that goes above and beyond the functionality that the System namespace provides."
~ Panopticon Central

"... the Microsoft.VisualBasic namespace is *NOT* the same thing as the Microsoft.VisualBasic.Compatability namespace."
~ AddressOf.com

Coming back to IIf(), it is still bad because in cases where we use objects with a possibility of containing null value, it will raise a NullReferenceException e.g.

Dim objIifReturn As Object = IIf(Not someObject Is Nothing, someObject.Value, Nothing)

From first look, it would appear that objIifReturn will contain someObject.Value if someObject is not null. However what happens is the dotNET runtime will evaluate someObject AND someObject.Value first. So let's say someObject is null, therefore when the runtime tries to evaluate someObject.Value, it will throw NullReferenceException instead of returning Nothing.

Friday, September 30, 2005

Serialization in dotNET

Well I never thought I would say this butttt..... serialization rocks man! I just discovered that it's quite easy to implement serialization with basic customization. All you need is to implement ISerializable in the base class.

For serialization, the method GetObjectData(ByVal info As SerializationInfo, ByVal context As StreamingContext) needs to be implemented.

For deserialization, the constructor New(ByVal info as SerializationInfo, ByVal context as StreamingContext) needs to be implemented.

Any customization of the serialization process is to be done within these two methods. A note of caution though, when implementing own serialization process, it is entirely possible to enable serialization of classes without the SerializableAttribute attribute. For example:

Public MustInherit Class BaseClass ...

Public Class FirstLevelClass Inherits BaseClass ...

Public Class SecondLevelClass Inherits BaseClass ...

It is possible to serialize FirstLevelClass even though it is NOT marked with the serializable attribute. As long as the highest level derived class is marked serializable, the serializer when called will attempt to serialize/deserialize.

Another requirement for own custom serialization is that each derived class MUST enable access to the constructor and the GetObjectData method. This depends on how the base class implements the method. If the GetObjectData is implemented in the base class as a public, then all derived classes do not need to reimplement it. However if the base class implements GetObjectData as protected, then each derived class needs to wrap it up and make a call to MyBase.GetObjectData(...).

The constructor on the other hand will always need to be implemented and wrapped around a call to MyBase.New(...).

Friday, August 19, 2005

Online Transaction Processing vs. Decision Support

Online Transaction Processing

Online Transaction processing database applications are optimal for managing changing data, and usually have a large number of users who will be simultaneously performing transactions that change real-time data. The primary concerns in this type of application are concurrency and atomicity. Concurrency controls in a database system ensure that two users cannot change the same data, or that one user cannot change a piece of data before another user is done with it. Atomicity ensures that all of the steps involved in a transaction complete successfully as a group.

Online Transaction Processing Design Considerations

  • Good data placement.
  • Short transactions.
  • Online backup.
  • High normalization of the database.
  • Little or no historical or aggregated data.
  • Careful use of indexes.
  • Optimum hardware configuration.

Decision Support

Decision-support database applications are optimal for data queries that do not change data.

Decision Support Design Considerations

  • Heavy indexing.
  • Denormalization of the database.
  • Use of a star or snowflake schema.

Wednesday, August 03, 2005

Caching Application Block

The Enterprise Library Caching Application Block lets developers incorporate a local cache in their applications. There are 3 purposes of caching application Block:

1. Performance: Caching improves application performance by storing relevant data as close as possible to the data consumer.
2. Scalability: Storing information in a cache helps save resources and increases scalability as the demands on the application increase.
3. Availability: By storing data in a local cache, the application may be able to survive system failures such as network latency, Web service problems, and hardware failures.

Local cache can be store in memory or in database. In memory cache is lost if the client machine fail. Database cache is unavailable if the network fail.

Below is the all the 5 functions of caching application block:

1. Adding items to the cache
2. Loading the cache
3. Flushing the cache
4. Removing items from the cache
5. Retrieving items from the cache

User Mode Scheduler (UMS)

User Mode Scheduler (UMS) is a feature in SQL Server 2000. UMS is used within SQL Server to schedule task. It is comparative to Windows Task Scheduler. Windows Task Scheduler is preemptive tasking which means that Windows prevents a single thread from monopolizing a processor. UMS, by contrast, relies on threads to yield voluntarily. If a SQL Server worker thread does not voluntarily yield, it will likely prevent other threads from running.

UMS serves as a thin layer between the server and the operating system that provides much of the same functionality offered by the Win32 thread and scheduling primitives, but it does so without requiring as many transitions into kernel mode or as many context switches.

Monday, July 25, 2005

Offline Smart Client Application Block

  • Detecting the presence or absence of network connectivity, thus enabling the application to behave according to its online or offline state
  • Caching the required data so that the application can continue to function even when the network connection is not available
  • Synchronizing the client application state and/or data with the server when the network connection becomes available

Wednesday, July 20, 2005

MSDE Setup

To setup MSDE

1. Run in your MSDE setup folder. securitymode=sql specifiy that people can access database using window authentication and also SQL authentication. disablenetworkprotocols=0 enable network access. sapwd is the password of login name sa.

2. Open TCP port in windows firewall. It is done in control panel windows firewall and add port in the exception tab page. The default prot number for MSDE is 1433

3. Download and install MSDE SP4. It is done by running the following command in the MSDE SP4 folder