Visual Studio 2003 and 2005 Default Keyboard Shortcuts Cheat Sheet

My first book, C# 4.0 How-To is now shipping! If you like tips you can use, check it out!

I wanted to have a quick-reference sheet available to help me learn the Visual Studio keyboard commands. Surprisingly, I couldn’t find a handy printable reference. So I made one. It doesn’t contain ALL the shortcuts. For that, you can go to the online reference or this code that prints out all of your existing shortcuts.

If you just want a two-page reference sheet, handy to print front-to-back, to help you learn the shortcuts, download the PDF I created.

A screen shot of it…

Visual Studio 2003 & 2005 Default Keyboard Shortcuts Screenshot

Queue multiple GUI updates into a single update event (C# .Net)

I’m writing a simple utility that involves scanning a hard disk and updating a display with the latest status. There are potentially many, many, many, many, MANY changes that happen to the display in very quick succession and sending an update event which triggers a screen refresh for every single state change would be an enormous bottleneck.

I implemented a simple queueing scenario where all the updated objects are queued for up to 200ms before being batched into a single event. The GUI can then update the display for every changed object all at once.

First, the queue:

private DateTime lastUpdateSent = DateTime.MinValue; //queue updated folders to limit the number of screen updates List<FolderObject> updatedFoldersQueue = new List<FolderObject>();

A simple list. Then I have a function which is called to handle the actual event generation:

private void FireUpdate(FolderObject folder) { if (this.OnFolderUpdated!=null) { try { if (!folder.Dirty) { folder.Dirty = true; updatedFoldersQueue.Add(folder); } TimeSpan diff = DateTime.Now - lastUpdateSent; if (diff.TotalMilliseconds > 200){ FolderUpdatedEventArgs args = new FolderUpdatedEventArgs(updatedFoldersQueue); int numFolders = updatedFoldersQueue.Count; //reinitialize queue updatedFoldersQueue = new List<FolderObject>(numFolders); OnFolderUpdated(this, args); Thread.Sleep(0); lastUpdateSent = DateTime.Now; } } catch (Exception ex) { System.Diagnostics.Trace.WriteLine(ex.Message); System.Diagnostics.Trace.WriteLine(ex.StackTrace); } } }

The folder.Dirty flag merely prevents a folder from being queued twice. The TimeSpan diff is the important bit. By checking the time difference between now and the last updated time, we can ensure that updates only get sent as often as the display–and our eyes–can handle them. The Thread.Sleep(0) ensures that the GUI thread gets a chance to draw–this is supposed to be interactive, after all.

Code formatter for Windows Live Writer

I stumbled across a great code formatter for Windows Live Writer today. Here’s an example, using a C# function that converts a number into a formatted file size:

       public static string SizeToString(long size) 
        { 
            const long kilobyte = 1L << 10; 
            const long megabyte = 1L << 20; 
            const long gigabyte = 1L << 30; 
            const long terabyte = 1L << 40; 
            string kbSuffix = "KB"; 
            string mbSuffix = "MB"; 
            string gbSuffix = "GB"; 
            string tbSuffix = "TB"; 
            string suffix = kbSuffix; 

            double divisor = kilobyte;//KB 
            if (size > 0.9 * terabyte) 
            { 
                divisor = terabyte; 
                suffix = tbSuffix; 
            } 
            else if (size > 0.9 * gigabyte) 
            { 
                divisor = gigabyte; 
                suffix = gbSuffix; 
            } 
            else if (size > 0.9 * megabyte) 
            { 
                divisor = megabyte; 
                suffix = mbSuffix; 
            } 

            double newSize = size / divisor; 
            return string.Format("{0:F2}{1}", newSize,suffix); 
        }

The power of the blog to motivate corporate, societal, and government change

This is an issue that has been discussed many times previously–so many that I won’t even bother to link to those discussions. By now it’s well-understood that blogs carry a power stronger than most in the media initially assumed possible.

Not just blogs, but the entire “Web 2.0” phenomenon–MySpace, YouTube–the whole rotten bunch. 🙂 Would Patricia Dunn have stepped down as chair of HP were it not for the constant pounding brought on by the likes of Scoble? Maybe, maybe not. In some sectors, blogs are becoming as well-regarded, if not more, than traditional publishing. Maybe this is limited to the computer industry. Maybe I just read too many blogs. 🙂

Still, it seems that the nature of debate and information dissemination has changed. No longer are we fed what mainstream publishers tell us–even if it’s of better quality. We are now free to choose what and how we read–for good or bad.

We’ve already seen the effects on the corporations. Companies simply can’t get away with anything anymore. Somebody, somewhere, will jump on it.

Areas where I think it will get more interesting:

  1. entertainment – RIAA, MPAA, I’m talking about you. You have ZERO friends among bloggers. All of the bad things you’ve done in courts to innocent people, all of your extortion is shouted from the rooftops by people like those at TechDirt.  You can’t win this war. For now, the audience isn’t very general, but news spreads, and it’s spreading faster and further. Sooner or later, you will lose the PR battle completely–in the meantime, unless your companies drastically change how they do business, your business will be swept out from under you, relegated to the dustbin of irrelevance.
  2. corporations – Microsoft already can’t do anything without the blogosphere lighting up. In some ways, they’ve chosen to embrace this–witness the very high-quality set of developer blogs they host. On the other hand, they’re like any other large company–they have secrets and tactics they would rather not be public debate-fodder. Corporations will be forced to open the windows and let the light shine in on what they’re doing. 
  3. government – imagine if honest, whistle-blowing (or even dishonest whistle-blowing!) staffers ratted on all the corruption in Washington. Imagine if every backroom deal was publicized in embarrassing detail. I don’t think we’re anywhere close to that yet, but there are signs that things are beginning to emerge. Look at the hilarity on YouTube about Senator Ted Stevens’ gaffe about the Internet’s tubes. How long as CSPAN been broadcasting, again? Our elected officials say dumb things about topics they don’t understand all the time–but now we can hear about it over and over again.

Overall, I think blogging will lead to more accountability of traditional structures of society. However, even with these possibilities, there are potential pitfalls:

  1. Overcrowded Medium — occurs when there are WAYYYYYYY too many people broadcasting that not enough people are listening. If everybody in the world blogged, who would read them?
  2. Loss of accountability – if there is accountability for things that are written online, than anything goes. The Internet is already the source of much bad information–it can become much worse if most of it is partisan, subjective, opinionated blather. Still, I’m not convinced it will really be worse than the status quo. The media now is far from infallible. Maybe part of me just wants to keep faith in people’s ability to reason. 🙂
  3. Undercrowded Debates – Broadcast media is a finite resource therefore it maintains its quality mostly by the fact that it has  to judge some things more worthy of discussion than others. Those topics are what people hear about. The Internet, on the other hand, is an unlimited resource. Anybody can have a blog on anything and most do. 🙂 This means that people themselves must choose what they follow, leading to some topics having far fewer meaningful discussions than others. For example, blogs about software and computers comprise a fairly large and active community. Politics has a large community. But what about small-interest, high-importance communities and topics? Where are the scientist blogs about global warming? I’m sure there are some, but is that kind of community ever going to gain a large enough population to affect societal opinion?
  4. Lack of participation – related to Undercrowded Debates, this means people don’t participate in all the areas that are pertinent to their lives. For example, how many of the US Internet users follow blogs discussing network neutrality? This is certainly an issue that could affect all of us, but from what I can tell it’s mostly debated on tech blogs, while the rest of the country misrepresents the entire issue. It works the other way around–I don’t read any political blogs at the moment. What issues am I missing out on? It’s too easy to become part of a niche community on the Internet and ignore the community as a whole.

Some of these problems stem from the anonymity of the Internet, others from the exponential increases in information available to us. Perhaps there are technologies in the pipeline that will solve these issues for us someday. They certainly aren’t going away.

tags: , , , ,

Code Formatter Plugin for Windows Live Writer

I stumbled across a great code formatter for Windows Live Writer today. Here’s an example, using a C# function that converts a number into a formatted file size:

       public static string SizeToString(long size) 
        { 
            const long kilobyte = 1L << 10; 
            const long megabyte = 1L << 20; 
            const long gigabyte = 1L << 30; 
            const long terabyte = 1L << 40; 
            string kbSuffix = "KB"; 
            string mbSuffix = "MB"; 
            string gbSuffix = "GB"; 
            string tbSuffix = "TB"; 
            string suffix = kbSuffix; 

            double divisor = kilobyte;//KB 
            if (size > 0.9 * terabyte) 
            { 
                divisor = terabyte; 
                suffix = tbSuffix; 
            } 
            else if (size > 0.9 * gigabyte) 
            { 
                divisor = gigabyte; 
                suffix = gbSuffix; 
            } 
            else if (size > 0.9 * megabyte) 
            { 
                divisor = megabyte; 
                suffix = mbSuffix; 
            } 

            double newSize = size / divisor; 
            return string.Format("{0:F2}{1}", newSize,suffix); 
        }

How to solve severe driver problems in Windows

A colleague at work recently got a second video card–a bottom of the barrel (or close to it) nVidia MX 4000 (PCI). He had an existing AGP nVidia Vanta. Well…the installation did not go well. It did something to Windows so that it consistently blue-screened during the driver load process (the progress bar moving in the startup splash screen).

Windows would start in safe mode, but removing the non-working drivers for the new card did not work. Removing both drivers did not work. Choosing last-known good configuration got us up and running in Windows (finally), but with only the bare VGA driver. Installing a driver from either CD or nVidia’s site ended in the strange error “Access Denied.”

Then I remembered what I had read in Windows Internals about the location of driver configuration information in the registry.  Driver info is stored with service configuration in HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services.

First we removed all hints of nVidia apps and videos drivers using Add/Remove Programs. Then we went into regedit, into the above key and deleted the keys “nv”, “nv4”, and “nvsvc” (I think they were those, but looking on my own machine at home, they’re a bit different, so I’m half-guessing). I’m sure there are similar keys for ATI chips.

In the meantime, we had found an unused AGP version of the MX 4000 just lying around (no joke), and replaced the Vanta with this. We reinstalled the drivers and everything worked great.

Don’t use CArchive with native C++ type bool

I recently ran into an issue where I was trying to serialize a bool variable from a CArchive object.

Archiving it out, with this code, works fine:

//CArchive ar;
bool bFill;
ar << bFill;

But this code:

ar >> bFill

has problems. It compiles fine under Visual Studio 2003, but errors out under Visual C++ 6 with this error:

C2679: binary ‘>>’
: no operator defined which takes a right-hand operand of type ‘bool’
(or there is no acceptable conversion)

Very weird. There is some explanation here.

My resolution? Use BOOL instead. Sure, it’s actually an int and takes 4 bytes, not 1, but that’s a not a big deal in this case. And it’s more clear than using BYTE.