The blog of flying mind

March 25, 2008

Apartments and Pumping in the CLR

Filed under: Software

Also see: Brad Abrams’ pixel8 Interview Podcast posted

I’ve already written the much-delayed blog on Hosting, but I can’t post it yet because it mentions a couple of new Whidbey features, which weren’t present in the PDC bits.  Obviously Microsoft doesn’t want to make product disclosures through my random blog articles.

< ?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /> 

I’m hoping this will be sorted out in another week or two.

 

While we’re waiting, I thought I would talk briefly(!) about pumping and apartments.  The CLR made some fundamental decisions about OLE, thread affinity, reentrancy and finalization.  These decisions have a significant impact on program correctness, server scalability, and compatibility with legacy (i.e. unmanaged) code.  So this is going to be a blog like the one on Shutdown from last August (see http://blogs.msdn.com/cbrumme/archive/2003/08/20/51504.aspx ).  There will be more detail than you probably care to know about one of the more frustrating parts of the Microsoft software stack.

(more…)

Natural Sorting in C#

Filed under: Software

Also see: Debugging an InvalidCastException

Jeff Atwood recently posted about natural sorting. This is all about making sure that strings that contain numbers sort numerically. I’m slightly surprised to see that he wants to call it alphabetical sorting. Surely by definition, alphabetical sorting is defined by, well, the alphabet. This is an issue about numbers, not letters.

Anyway, he says he tried and gave up on a succinct C# version. He suggests that it will take 40+ lines of code. I believe that’s misleading, because as far as I can tell, the Python versions are only able to be so succinct because Python already appears to know how to sort an array. Both examples he shows rely on this. In.NET, collections aren’t intrinsically sortable. Let’s sort that:

/// <summary>
/// Compares two sequences.
/// </summary>
/// <typeparam name=”T”>Type of item in the sequences.</typeparam>
/// <remarks>
/// Compares elements from the two input sequences in turn. If we
/// run out of list before finding unequal elements, then the shorter
/// list is deemed to be the lesser list.
/// </remarks>
public class EnumerableComparer<T> : IComparer<IEnumerable<T>>
{
 /// <summary>
 /// Create a sequence comparer using the default comparer for T.
 /// </summary>
 public EnumerableComparer()
 {
 comp = Comparer<T>.Default;
 }
	
 /// <summary>
 /// Create a sequence comparer, using the specified item comparer
 /// for T.
 /// </summary>
 /// <param name=”comparer”>Comparer for comparing each pair of
 /// items from the sequences.</param>
 public EnumerableComparer(IComparer<T> comparer)
 {
 comp = comparer;
 }
	
 /// <summary>
 /// Object used for comparing each element.
 /// </summary>
 private IComparer<T> comp;
	
 /// <summary>
 /// Compare two sequences of T.
 /// </summary>
 /// <param name=”x”>First sequence.</param>
 /// <param name=”y”>Second sequence.</param>
 public int Compare(IEnumerable<T> x, IEnumerable<T> y)
 {
 using (IEnumerator<T> leftIt = x.GetEnumerator())
 using (IEnumerator<T> rightIt = y.GetEnumerator())
 {
 while (true)
 {
 bool left = leftIt.MoveNext();
 bool right = rightIt.MoveNext();
	
 if (!(left || right)) return 0;
	
 if (!left) return -1;
 if (!right) return 1;
	
 int itemResult = comp.Compare(leftIt.Current, rightIt.Current);
 if (itemResult != 0) return itemResult;
 }
 }
 }
}
	

(more…)

Updated Finalization and Hosting

Filed under: Software

Also see: Memory Model

My original posts on Finalization and Hosting had some hokey XXXXX markers in place of content, where that content hadn’t already been disclosed in some form.  Now that the Visual Studio 2005 Community Preview is available, I’ve gone back to those two posts and replaced the XXXXX markers with real text.

Also, it’s obviously been a while since my last post.  I started writing something this weekend, but the weather here has been spectacular and I was compelled to go outside and play.  I’ll try to have something in the next couple of weeks.

 


http://blogs.msdn.com/cbrumme/archive/2004/04/26/120609.aspx






















Get free blog up and running in minutes with Blogsome
Theme designed by Hadley Wickham