Sunday, 23 September 2012

The Importance of Managed Debugging Agents (MDAs)

MDAs work with CLR to provide information on runtime state. One of the most famous MDAs is loaderLock MDA.

The idea of MDAs is to isolate hard-to-find app-bugs when transitioned from managed to unmanaged code.

Thursday, 13 September 2012

MSVS 2010 Autoformat Code

Cntrl-E Cntrl-D.
(Eclipse: Cntrl-shift-F)

WPF Multibindings

Bind multiple items and return a single value.

Excellent functionality in all kinds of control panel scenarios.

The WPF DataGrid - Explained

It might seem mysterious how one can populate a WPF DataGrid from System.Windows.Controls, the "Fourth" Incarnation of the DataGrid.

Answer: Create your own ObservableCollection

All lies in setting the ItemsSource property to an ObservableCollection of custom-built ViewModel objects. As we know from MVVM design, the ViewModel is just like a "screen" on top of the Model that keeps the Model up to date and is also responsible for notifications when properties change, to the relevant observers.

From ObservableCollection to NotifiableCollection

ObservableCollection is good for dealing with "macro" changes to the Collection (as when an Item gets added or deleted) but is not good at detecting and propagating changes when properties on individual items change. For that, a custom-built NotifiableCollection is appropriate (see this discussion on forums.silverlight.net (general principle applies to WPF also)) and also this blog post from a software consulting firm specialising in "nearshoring").

Declaring the NotifiableCollection has to be done in "Generic" Style

public class NotifiableCollection<T>: ObservableCollection<T> where T: class, INotifyPropertyChanged {... }

Note that you also need to declare an EventHandler within the Collection definition, which can be simply done: public event EventHandler<MyEventArgsClass> InterestingPropertiesChanged, or some such similar declaration. Conceptually, this is like defining a multicast delegate to hold the function pointers representing methods to be invoked when an "interesting" property is changed.

Conclusions

The main conclusion is this.

It's nice to know what you can do in WPF using System.Windows.Controls. But a knowledge of controls alone is no use without working knowledge of the relevant object model and component model namespaces.

Thes are System.ComponentModel (INotifyPropertyChanged) and System.Collections.ObjectModel (ObservableCollection). Understand the ComponentModel and Collections Object Model in detail.

Tuesday, 11 September 2012

Respect to System.Windows.Input

For it brings us the RoutedCommand.  RoutedCommand is a central pillar to Commanding in WPF and implements the ICommand interface (also part of System.Windows.Input) with which every .NET programmer should be really familiar with. ICommand? Oh yeah, that old thing. That means the RoutedCommand has to implement Execute and CanExecute, is all. And not just RoutedCommands, either. Also RoutedUICommands, which is, like, a special case of a RoutedCommand. I won't explain the differences between the various RoutedCommands. Let experience be your teacher.

Recap: Name three classes in System.Windows.Input related to Commanding. Hint: See Above.

But one thing we must mention.

System.Windows.Input ain't just about Commanding. 

Oh no, there's a whole PLETHORA of crazy stuff in there. Everything to do with the WPF input system, including keyboard, mouse and stylus input processing types. One such crazy class would be e.g. StylusButton, that represents a button on a stylus. Much cooler than going pound-include iostream; and all that malarkey in C++. Forget C++. This is the Age of C#.

Monday, 13 August 2012

Art of C# Sockets II: "Lineage" of NetworkStream

What is a NetworkStream? Specifically what makes it unique from its abstract base class Stream.

A network stream is like a stream of water, only instead of H20 going from the top of the mountain to the valley below, we instead have "molecules" of data (which we call bytes)  travelling continuously from a source to a destination. If the source and desination are at different parts of a computer network, the connecting byte stream can be loosely termed a "NetworkStream".

Ok, so we know vaguely know what a Network Stream, as they call it, is all about. Where does the NetworkStream class come from? What's its lineage, so to speak, in terms of the object model/class hierarchy.

Stream is the abstract base class of all Streams (the top of the Himalayas, if you will). Stream is the only class that NetworkStream directly subclasses from. Indirectly, NetworkStream also inherits from MarshalByRefObject and System.Object but these are rather peripheral facts (we'll look at the esoteric implications much later).

NetworkStream is a specialization of a Stream that supports reading, writing and seeking for Streams over the Network and lives in the System.Net.Sockets namespace. The class is very useful when writing TCP clients and servers.

NetworkStream extends System.IO.Stream. Has methods to read and write byte arrays. Streams also provide seeking capabilities - ability to query and modify the current position within a stream.

What is the practical usage of NetworkStream?

To send messages from TCP client to server.

C# Sockets Programming (Intro to System.Net.Sockets)

Desperate Programmer: I want to write network software and not be bound by weirdo libraries that limit me in how many connections I can make and such like.
Wise Programmer: Then what you need is Sockets. Master sockets and cross the impasse of data transfer across networks.
Adage: Don't trust a Dot Net Programmer to architect an application who has never programmed network software in C# and moreover has never programmed Sockets.
Desperate Programmer: So, then how do I unlock the Sockets functionality of .Net?
Wise Programmer: Start with the System.Net.Sockets namespace and progress from there

Concept Check: What is the Relation between TCP and Sockets?

Socket is a SOFTWARE OBJECT -> that REPRESENTS an endpoint for inter-process communication. Sockets can use various PROTOCOLS (rulesets), such as TCP and UDP.

How do you create a TCP Client in C#? Tell me PRECISELY.

The answer is SIMPLE. You can guess it without knowing C#. Use the TcpClient class. Where does that great TCP client class live? Ans: System.Net.Sockets (part of System.dll).

Suppose your TCP client is aptly named client. To connect the client to a socket, you need to do client.Connect(IPEndPoint). The endpoint consists of an IP address and a port. Now we have a connection. The server has to be up though for this connection to work.

How do you send a message from the Client to the Server?

You need to grab the NetworkStream from the TcpClient.  You can do stuff with the Stream, like write byte arrays to it, and Flush it. To convert a string to a byte array, you can use the class ASCIIEncoding (System.Text, from mscorlib.dll) which represents an encoder. Calling GetBytes on the encoder creates a byte buffer.

How do you create a TCP Server in C#? Again, a precise explanation is requested.
This is just a TcpListener. You create a TcpListener using IPAddress.Any, and the required port.  Calling Start() on the listener starts it listening to incoming requests. The method AcceptTcpClient returns the next TCP client accepted by the listener. Once you have the client object, you can spawn a new thread to handle the client communication.

Writing a TCPClient is child's play. Writing a TCP server is a little more involved.