Sunday 29 September 2013

.NET AppDomains and why Threads are Special

.NET AppDomains and Their Relationship to Processes, Assemblies and Threads

An Application Domain , or AppDomain, in .NET, provides a boundary wall between running .NET applications, to enhance reliability and security. They are like discrete pinball machines.

AppDomains and Processes; Plus Points of Partitioning Arrangement

AppDomains are structured such that several of these domains may be run in a single process; in math terms, the process space is partitioned into AppDomain space i.e. can have several of these non-intersecting AppDomains that make up the process space. This avoids the "resident" applications from the overhead of making inter-process calls when they need to communicate (everyone is in the same process "locale").

AppDomains and Assemblies

A .NET assembly must be loaded into an AppDomain in order to be able to run. A typical application will load several assemblies into a single AppDomain.

AppDomains and Threads

A thread must run in an AppDomain, but has the ability to cross into other AppDomains. They are bit like electrons within atoms.(Thread.GetDomain will tell you what AppDomain a thread is currently running in).

Alt-V-O- The Output Window of Visual Studio

The Output Window in Visual Studio shows Output from the Build.

Alt-V-O is the shortcut to get it up - in case you have closed it by accident, or to create extra space.  If you find it hard to read, try clicking "Toggle Word Wrap" (the right-most button on the Output Window toolbar that looks like a "carriage return").

Writing a Function that Never Returns in C#

Unfortunately, there is no standard way of declaring this e.g. "public never FunctionThatNeverReturns()" is one possible syntax but is not part of standard C#.

In fact, it's not part of standard C++ either. However, some compilers, such as GCC, have a way to express this by declaring function attributes which are enclosed within double round brackets.

void fatal() __attribute__ ((noreturn)) { ... }

Unfortunately, adding this attribute does not mean the function will not return control to the caller in exceptional cases, e.g. when an exception is thrown or a longjmp is attempted.

vshost can improve debugging speed but can get locked!

The vshost file was introduced in Visual Studio 2005 to make debugging launch quicker. The vshost.exe that is created as part of your application is also known as the "Hosting Process". They are not designed to be run directly or included as part of a deployment. Its performance boost comes from having an application domain already created, that the debugger gets associated with.

Sometimes the vshost file gets locked. You need to close the Visual Studio with that project opened to unlock it.

Saturday 28 September 2013

Don't Forget the Computer in Computer Science

As Peter Norvig, author of "AI - A Modern Approach" reminds us, we should not forget the "computer" in computer science. For example, having a handle on how long branch misprediction takes on a "typical" PC (hint: it's in nanoseconds), or a fetch from an L1 cache versus a fetch from an L2 cache (hint: big difference). We might note that mutex lock/unlock is pretty expensive in contrast to the aforementioned operations.

Monday 23 September 2013

The Standard Way to Install a Windows Service from the Command Line

Step One: Open a command window with Administrator privileges. Cd into the appropriate .NET framework directory probably C:\windows\framework\microsoft.net and so forth. Run installutil on your exe and you're done! Installutil /u does an uninstall - in case you change your window.