Posts for #C#

I did that presentation there yesterday. I had added a piece on top, to stretch things a bit more in the direction of WCF: the Game Status Viewer uses an additional published service to query game status information and displays that in a console window.

Here are the slides and samples in that most current version:

If you’re in Germany, here’s (hopefully) interesting news: starting from issue 11, my column “C# Corner” will be a regular feature in dot.net magazin. I’m looking forward to writing articles for that, on new and interesting things going on around the C# language, as well as best practices and whatever else comes to mind, really 😃.

I did a presentation recently at the NxtGenUG in Oxford, where I introduced WPF and WCF in a combined session. I created a network enabled implementation of Connect Four to do this. Here’s the download of the demo solution, as requested by some of the attendees:

I have this sample from a recent talk at Basta! conference in Germany, which shows (among other things) how to bind a cross table to a DataGridView (the standard .NET 2 data grid). A cross table is basically the result of transposing some data and using one of the fields for a second dimension.

The sample I have works on two tables of data. It doesn’t actually use a database and the reference that points from the list of votes to that of features is implemented as an object reference, but the relationship is like this:

I recently saw a post in an MS newsgroup and as I had an example that I wrote earlier, I just thought I’d make it available. What this does is simple: it implements custom handling of the non-client area of a window. The non-client area is, normally, the area of a window that is not part of the client area (duh!), like that used for adornments, window buttons, borders, that kind of thing. While the user moves the mouse over a window’s area, Windows sends messages to figure out which part of the window is currently underneath the mouse cursor, and if it finds certain areas, it handles the corresponding functionality itself.

By overriding the default behaviour and returning the right values for these messages, it’s therefore easy to have Windows activate the resize behaviour of the right border, for instance, although the mouse cursor is actually not over the right border at that moment (or even if there isn’t any right border at all). My sample program draws some rects on its client area that “pose” as the borders of the window, as well as the title bar and the resize rect in the lower right hand corner. Looks like this:

In two previous posts (here and here) I have previously posted about how to draw rubber band selection rectangles similar to what Explorer does. Now a reader contacted me by email and asked for an extension: he wants to have a reverse selection, similar to this image:

In a newsgroup I replied to a question about calculating average data throughput during data reception over the network. A simple average bytes per second calculation needs only a few lines of code:

int bytesTotal = 0;
int bytesPerSecond = 0;
DateTime startTime = DateTime.Now;

do {
  bytesRead = receiveStream.Read( ... );
  bytesTotal += bytesRead;
  bytesPerSecond = bytesTotal / (DateTime.Now - startTime).TotalSeconds;

  ...
} while(bytesRead > 0);

This is the eighth article in my mini series about object pooling. Be sure to read part 1, part 2, part 3, part 4, part 5, part 6 and part 7 first. So, although the number of downloads of the sample program hasn’t been exactly great, I’m finally continuing the series. As I said in a previous article, I’d like to factor out the resizing behaviour – but before I do that, I want to integrate functionality that’ll let me evaluate the pool’s performance with regard to resizing, so I can assess the different resizing approaches reliably. Fortunately performance monitoring is something that needs to be integrated into the pool implementation anyway and Windows has all the tools on board. So I only need to configure a few performance counters in Windows, make sure that these counters are updated with current numbers and the Windows performance monitoring frontend is available to the user to show it all in a nice graph. (I’m sure some of you have never heard of the performance monitor application – it’s available on the Administrative tools menu and it’s called, not surprisingly, Performance.) Here’s the download for the current version, including these changes: ObjectPooling-2.zip

Just saw this post in a newsgroup about how to show the horizontal lines that Microsoft likes to use in their dialogs. A lot of people replied with ideas of using other controls, like panels, to achieve the desired effect – to me, a much simpler and more effective idea is to just create a control myself. So, the specific style described by the OP seems to be this:

This is the seventh article in my mini series about object pooling. Be sure to read part 1, part 2, part 3, part 4, part 5 and part 6 first. As some of you may have noticed, I introduced a bad bug in the last article about growing and shrinking, specifically in the code that would call the ExtendPoolBy and ShrinkPoolBy methods. The amount by which to grow or shrink the pool was calculated as the difference between two percentage figures and passed in to the two methods, which really expected absolute values! That’s what I get for not using test-first development on this 😃 So I fixed this and introduced two simple algorithms instead to handle the calculation of the growing and shrinking amounts. This is quite an important topic for object pooling, because it defines how well the pool scales in various scenarios – sometimes it may be desirable to have a pool that adapts quickly to new requirements, sometimes it’s more important to keep the pool size stable as long as possible. In one of the next posts in the series I’m going to factor out the growing/shrinking behaviour using the strategy pattern, until then the current calculation will have to do – and the new test program shows that it doesn’t do that bad at all!

The test program