Category Archives: Code

Measure Amount of Data to Serialize with a Null Stream

If you’ve got to serialize some data, especially in a binary format, it’s common to output the length of the data. This is useful for versioning, random access, knowing when you’re done reading the records, among other reasons. Therefore, you need to know the size of the data you’re going to serialize. There are a [...]

C# 4.0 How-To Available Now!

Well, it’s finally out! Amazon no longer lists the book as available for pre-sale, and it should be shipping to purchasers today or tomorrow. If you’re a B&N shopper, you can also order it there, or grab it in stores within a few days. From the product description: Real Solutions for C# 4.0 Programmers Need [...]

How to position windows correctly at 120 DPI in WPF

If you want to place a window at a specific place in WPF, it will work pretty much as you expect—unless your DPI is 120 (the default is 96). Here’s a sample that shows how to put it where you want. In this case, I want to put a window just under another control, aligned [...]

A WPF Numeric Entry Control

When WPF first shipped, there was a noticeable lack of certain controls we’ve become used to in Win32 and WinForms: Calendar, DateTimePicker, and NumericUpDown. WPF 4 adds Calendar and DatePicker, but not anything for numeric entry. For my solution I wanted something that behaved very similarly to the WinForms NumericUpdown control. Some of the specifications: [...]

Updated CPU usage article

I made a important changes to the CPU usage code and have updated the article to reflect it. Instead of a critical section, the code uses just the interlocked increment/decrement functions. I also updated the sample demo to use multiple threads to read the CPU usage to demonstrate the thread safety clearly.

Determine CPU usage of current process (C++ and C#)

My first book, C# 4.0 How-To is now shipping! If you like tips you can use, check it out! Updated 2/4/2009: I changed the implementation of these classes from the original: Instead of a critical section, InterlockedIncrement/Decrement is used. The sample driver program now demos using multiple threads using the CpuUsage class to show thread [...]

An easy stack layout panel for WinForms

This is a simple, but useful tip. Users of WPF are spoiled. They have all sorts of layout options. Those of us still working in WinForms have FlowLayoutPanel and TableLayoutPanel. That’s it. WPF has those and more. For my current project, I needed a panel to layout controls vertically. The TableLayoutPanel can be awkward to [...]

Converting OLE_COLOR to System.Drawing.Color

I’ve been working on a project using Visual Studio Tools for Office 2008 (VSTO) and at one point I needed to get the colors for categories in Outlook 2007. There are actually 3 colors, and they are returned as uint’s–why the .Net wrappers don’t convert to colors for you, I don’t know (to avoid linking [...]

Tracking database changes using triggers

Tracking changes in database tables is an incredibly useful feature–especially for operational data that can change often. Having recently had to implement this feature, I thought I’d share some of the techniques I learned. Sample Database First, let’s conceptualize a very simple database consisting of user information (name, date of birth), and e-mails. A user [...]

In this universe we obey the law of commutativity

This kind of thing has happened to be a few times now, so I thought I’d share the fun. In one of our pieces of software we have a process that looks like this: void MyThread() { while (true) { DoFunctionA(); DoFunctionB(); SleepFor10Seconds(); } } While FunctionA and FunctionB are conceptually similar, they interact with [...]