The Interop dream
WSDL.exe Sucks
- You do some databinding stuff
- Or add some validations on the accessor methods
After googling, the following tools generate a class with getter setter methods.
XSDObjectGen: This generates objects from a XML Schema. It generates property accessors backed up by public fields. It adds an XmlIgnore attribute on the generated property accessors which can be used by your application.
WSContractFirst: A Visual Studio Addin and Command Line utility to enable Contract (WSDL/XSD) first web services development.
svcutil: A Windows Communication Foundation tool. Though this generates classes targetted to be used from WCF this can be customized a bit.
WSContractFirst looks like a good solution to my requirement.
Also on the list is the WSDL tool customized from Mono by Brian Ritchie. But the download link seems to be down. Anyway WSContractFirst looks like a good option. Anything like a CodeSmith Template on this side which is more customizable will be great, and I am googling without much luck on this.
Talking about How to Use Design Patterns? A Discussion with GAMMA
Prakash is linking to some great stuff on Artima. A good read.
Quote
How to Use Design Patterns? A Discussion with GAMMA
Among developers, design patterns are a popular way to think about design, but what is the proper way to think about design patterns? In this interview, Erich Gamma, co-author of the landmark book, Design Patterns, talks with Bill Venners about the right way to think about and use design patterns.More here….
Talking about How to learn, write and think more clearly?
Nice entry from Prakash. It takes atleast 30 minutes though
Quote
How to learn, write and think more clearly?
It is well worth ten minutes as it tells you how to learn, write and think more clearly
Waiting for something to happen – Manual and AutoResetEvents
A cross process synchronization – Mutex
Waiting for something to happen – WaitHandles
Assume two threads want to acquire a lock. Say thread 1 aquires it first. Thread 2 would like to know when Thread 1 has released the lock. This is the second case of inter thread communication issue which we discussed in the Multi-Threading basics – Setting the state article.
One way is to poll continuously to check if Thread 1 has released the lock. This solution would result in wasting precious CPU cycles. There should be some way to make a thread wait till someting of interest happens and be notified of the event of interest. The waiting thread becomes notified and awakens when the event occurs without resorting to polling.
As we discussed earlier kernel objects are either signalled or non-signalled. The Win32 API WaitForSingleObject puts the calling thread into Wait till the kernel object passed becomes signalled. If you call WaitForSingleObject passing a signalled object, the calling thread can run. If you call WaitForSingleObject passing a non-signalled kernel object the calling thread has to wait till the object becomes signalled.
The same is encapsulated in the object oriented .NET Framework by the WaitHandle class. A WaitHandle can be either in a signalled or non-signalled state. WaitOne stands in as a equivalent for the WaitForSingleObject Win32 API. If you think the discussion is at a too abstract level then that’s because this WaitHandle is an Abstract class. Mutex, AutoResetEvent and ManualResetEvent are concrete classes which derive from Waithandle and provide you waiting abilities.
Critical Sections of code – Monitor
Thread A | Thread B |
writes the source | Suspended |
Context-Switch | Context-Switch |
Suspended | writes the source |
Context-Switch | Context-Switch |
writes the datetime | Suspended |
Context-Switch | Context-Switch |
Suspended | writes the datetime |
Context-Switch | Context-Switch |
writes the Category | Suspended |
Context-Switch | Context-Switch |
Suspended | writes the Category |
Context-Switch | Context-Switch |
writes the message | Suspended |
Context-Switch | Context-Switch |
Suspended | writes the message |
Context-Switch | Context-Switch |
The result will be messy due to the out of order execution.
How do you avoid this data corruption?
This can be avoided if we can ensure that only one thread executes this ‘critical section’ of code at a time. Assoicate this section of code with a lock object. Lock the object. Execute this section of code. Release the lock. If another thread tries to acquire a lock it has to wait till the this thread releases a lock. This can be achieved in .NET using the Monitor class. Let us rewrite the code using the Monitor class.
Atomic access functions – Interlocked family of functions
public class QueueHandler
{
private int itemsInQueue;
public void ProcessItem();
{
// Process Item
// Decrement itemsInQueue
Interlocked.Decrement(ref itemsInQueue);
if (itemsInQueue != 0)
{
// Queue another work item for ProcessItem
}
}
}
Other Interlocked members:
public static int Decrement(ref int);
public static long Decrement(ref long);
public static int Increment(ref int);
public static long Increment(ref long);
public static int Exchange(ref int, int);
public static object Exchange(ref object, object);
public static float Exchange(ref float, float);
public static int CompareExchange(ref int, int, int);
public static object CompareExchange(ref object, object, object);
public static float CompareExchange(ref float, float, float);
Exchange
Sets an object/int/float to a specified value as an atomic operation, and returns a reference to the original object/int/float.
CompareExchange
Compares two objects for equality and, if they are equal, replaces one of the objects.
What do we do if our critical section of the code (code which manipulates the shared data – just the decrement in this case) is not just decrement, increment, swap or compare and swap? How do we avoid races in such a case? Let us see this with an example in an upcoming article.
Thread States in .NET
What are the various thread states?
The following diagram gives an idea of the various thread states and the transition between them.
Action | ThreadState |
A thread is created within the common language runtime. | Unstarted |
A thread calls Start The thread starts running. | Running |
The thread calls Sleep | WaitSleepJoin |
The thread calls Wait on another object. | WaitSleepJoin |
The thread calls Join on another thread. | WaitSleepJoin |
Another thread calls Interrupt | Running |
Another thread calls Suspend | SuspendRequested |
The thread responds to a Suspend request. | Suspended |
Another thread calls Resume | Running |
Another thread calls Abort | AbortRequested |
The thread responds to a Abort request. | Stopped |
A thread is terminated. | Stopped |
A note on the background threads:
The frmaework categorizes threads into two flavours background and foreground. This is controlled by the IsBackground property on a thread. When all the Foreground threads in a process terminate the process terminates irrespective of whether background threads are running or not. When atleast one foreground thread is there, the process cannot terminate. When there are no foreground threads even if there are background threads running the process terminates.