From Azure Autoscaling to Next Gen Desktop Development not Forgetting Firmware on the Way
Sunday, 22 February 2009
Becoming A Windows Boxing Champion
A famous boxer once said: "Champions aren't made in gyms. Champions are made from something they have deep inside them - a desire, a dream, a vision. They have to have the skill and the will. But the will must be stronger than the skill". Put up your hands, Windows Joe is in the house.
Monday, 16 February 2009
Threading Basics in Dot Net
In C#, before you use threads, consider whether you can do the same thing just using events or delegates (i.e. built-in, implicit threading).
If you want to create threads explicitly, you can use System.Threading namespace. Useful stuff in this namespace include the ThreadPool class, and the Timer class, which will execute callbacks on thread pool threads.
The Thread class is a sealed class i.e. cannot be subclassed (structs, can be used for lighweight objects e.g. Point, Color, can are implicitly sealed. You cannot subclass a struct).
Thread th = new Thread( new ThreadStart(display) ); // display is a method
th.IsBackground = true;
th.Start();
An interesting article on deadlock in C# is here.
If you want to create threads explicitly, you can use System.Threading namespace. Useful stuff in this namespace include the ThreadPool class, and the Timer class, which will execute callbacks on thread pool threads.
The Thread class is a sealed class i.e. cannot be subclassed (structs, can be used for lighweight objects e.g. Point, Color, can are implicitly sealed. You cannot subclass a struct).
Thread th = new Thread( new ThreadStart(display) ); // display is a method
th.IsBackground = true;
th.Start();
An interesting article on deadlock in C# is here.
Sunday, 15 February 2009
The Asynchronous Art of the Dot Net Delegate (BeginInvoke, EndInvoke etc)
Custom Event Handling Techniques Using Delegates (together with event keyword)
Suppose you have a combo box that, on changing state, wishes to send an event to other windows (a kind of internal messaging, if you will). You can define an event handler as a delegate, which takes 2 variables (an object representing the sender, and a custom "event args" object, which encapsulates the state of the event).
Then can explicitly declare the event using the delegate: "public event ClickMeDelegate clickChange" and a method to "fire" the event. All "listeners" register on this event by adding and instantiating new event handler objects.
Declaring, Instantiating and Invoking a Delegate
Note that I DON't say C# delegate, as multicast delegates are found across the .Net framework - e.g. they are also found in Visual Basic .NET and Visual J#. However we will use C# as the lingua franca in which to present delegates.
A delegate in C# is an objectified function pointer. They are .NETiquitous in two ways:
1. EVENT HANDLING
2. ASYNCHRONOUS EXECUTION
To declare a delegate use the following syntax:
public delegate void MyDelegate(string message);
Now MyDelegate becomes a reference type which can encapsulate a named or anonymous method. Now we can define a method "MyDelegateImpl" which matches the signature.
MyDelegate m1 = MyDelegateImpl; // instantiate delegate with NAMED method
MyDelegate m2 = delegate( string msg ) { do s/t; }; // ANONYMOUS method
// invoke
m1("ya"); m2("hoo");
Difference with Multicast Delegates
Maintains an invocation list (implemented as a linked list) of methods it is bound to. The method Combine adds a method to the invocation list and Remove removes it. Combine is used for creating event handlers, that call multiple methods when an event occurs. The binary operators +, - can be used as stand-in for Combine and Remove as this example shows.
It should be strongly emphasised that +,- do not change existing delegates but rather return a delegate representing the result.
Key classes are System.Delegate and System.MulticastDelegate, both part of mscorlib.dll. Multicast delegates can be used to implement the Observer design pattern.
What declaring a multicast delegate entails
When you declare an MCD the CLR creates a class that extends System.MCD, and adds the methods: Invoke, BeginInvoke and EndInvoke. Invoke is what the CLR calls during synchronous execution. To execute asynchronously, use BeginInvoke which is non-blocking and returns an IASyncResult reference. When you call EndInvoke to get the result (this is a blocking call) you need to pass in the reference.
Suppose you have a combo box that, on changing state, wishes to send an event to other windows (a kind of internal messaging, if you will). You can define an event handler as a delegate, which takes 2 variables (an object representing the sender, and a custom "event args" object, which encapsulates the state of the event).
Then can explicitly declare the event using the delegate: "public event ClickMeDelegate clickChange" and a method to "fire" the event. All "listeners" register on this event by adding and instantiating new event handler objects.
Declaring, Instantiating and Invoking a Delegate
Note that I DON't say C# delegate, as multicast delegates are found across the .Net framework - e.g. they are also found in Visual Basic .NET and Visual J#. However we will use C# as the lingua franca in which to present delegates.
A delegate in C# is an objectified function pointer. They are .NETiquitous in two ways:
1. EVENT HANDLING
2. ASYNCHRONOUS EXECUTION
To declare a delegate use the following syntax:
public delegate void MyDelegate(string message);
Now MyDelegate becomes a reference type which can encapsulate a named or anonymous method. Now we can define a method "MyDelegateImpl" which matches the signature.
MyDelegate m1 = MyDelegateImpl; // instantiate delegate with NAMED method
MyDelegate m2 = delegate( string msg ) { do s/t; }; // ANONYMOUS method
// invoke
m1("ya"); m2("hoo");
Difference with Multicast Delegates
Maintains an invocation list (implemented as a linked list) of methods it is bound to. The method Combine adds a method to the invocation list and Remove removes it. Combine is used for creating event handlers, that call multiple methods when an event occurs. The binary operators +, - can be used as stand-in for Combine and Remove as this example shows.
It should be strongly emphasised that +,- do not change existing delegates but rather return a delegate representing the result.
Key classes are System.Delegate and System.MulticastDelegate, both part of mscorlib.dll. Multicast delegates can be used to implement the Observer design pattern.
What declaring a multicast delegate entails
When you declare an MCD the CLR creates a class that extends System.MCD, and adds the methods: Invoke, BeginInvoke and EndInvoke. Invoke is what the CLR calls during synchronous execution. To execute asynchronously, use BeginInvoke which is non-blocking and returns an IASyncResult reference. When you call EndInvoke to get the result (this is a blocking call) you need to pass in the reference.
Tuesday, 3 February 2009
The History and the Future - Acrimonious Hacking on Windows
"Acrimonious hacking" involves hacking with acronyms. Certain programming experts assert that the best hackers think in acronymns. Take Simonyi's Hungarian notation for example. Even in verbose codebases, the best hackers mentally abbreviate objects and varnames into condensed versions they can manipulate quickly in their brain and perform rapid mental refactorings. Victory be to the acrimonious hackers!
"Acrimonious hacking" is an attempt to stem the complexity of imperative programming.
A "higher level" attempt to manage imperative-prog complexities is "intentional programming".
The creed is to separate the problem from the program, and to use generative techniques to create the program on the computer. A whole website on code generation, the "code generation network" describes projects in .Net, Java and MDA. MDA is a set of OMG standards and a special report is here.
"Acrimonious hacking" is an attempt to stem the complexity of imperative programming.
A "higher level" attempt to manage imperative-prog complexities is "intentional programming".
The creed is to separate the problem from the program, and to use generative techniques to create the program on the computer. A whole website on code generation, the "code generation network" describes projects in .Net, Java and MDA. MDA is a set of OMG standards and a special report is here.
Subscribe to:
Posts (Atom)