First time here? Check out the site's "greatest hits" or read a post from the archives. Feel free to leave a comment or ask a question, and consider subscribing to the latest posts via RSS or e-mail. Thanks for visiting!
Do you Tweet? Follow me on Twitter @shanselman or learn how to use Twitter!
« Connectify creates and shares Virtual Wi... | Main | Windows 7 with BitLocker and Still Booti... »

imageIt's funny to watch things go viral, even just a little viral on the Internet. Here's what happened, but more importantly, we'll talk about the code. Let's also make it complete clear that Jeff Key rocks. See picture at left, in between his two "lame" creations."

First, I did a post earlier this week called "Light it Up: List of Applications that use new Windows 7 Features." A day or two later I got an instant message from my former-roommate and part-time belay Jeff Key (@JeffreyKey on Twitter) (actually, that's all a complete lie, but, Jeff and I are friendly acquaintances for many years and have each other on IM) that said:

Saw your Win7 features post yesterday, so whipped this up last night and posted it on codeplex this morning:

http://taskbarmeters.codeplex.com/ kind of lame, but that's how i roll

Jeff Key jeff.key@sliver.com

For years Jeff has lived the mantra "Talk is Cheap, Show Me the Code." And he does, with some of the most inspired little .NET-based utilities out there asking for little else but our undying admiration and gratitude. That is how Jeff rolls. I visited his CodePlex site and saw it had 11 downloads.

image

I tweeted it and forgot about it. Then that tweet got picked up by Download.com (which I've heard of and whole gave credit to Jeff) Life Rocks 2.0 (which I've never heard of and who gave credit to no one) and then Lifehacker (which I have heard of and who "via'ed" Life Rocks). Next, I returned to CodePlex and saw that it had 4152 downloads! Congrats to Jeff for being so "lame!" ;)

image 

The Code

Why would Jeff be so down on himself and say the code is "lame" when clearly people were (are) going bananas and downloading these little utils? Well, because it's so darn easy to do, this was likely the source of Jeff's intense guilt. ;) The Windows API Code Pack makes it easy.

ASIDE: In fact, WPF on .NET 4 makes it even easier because it includes the new TaskbarItemInfo class that lets you do this from XAML. Pete Brown from my team has a great write-up on Showing Progress in the Windows 7 Taskbar with WPF 4 on his blog.

First, since his apps are specific to Windows 7, he checks first to make sure it's OK to continue. Note that it IS very possible to make apps that work great from XP to Windows 7, but these apps are little Windows 7 showcases, so you can see why he'd want to check for this:

if (!TaskbarManager.IsPlatformSupported)
{
MessageBox.Show("Sorry, but this app only works on Window 7.", "Aw snap!", MessageBoxButton.OK, MessageBoxImage.Error);
Application.Current.Shutdown();
}

To update the Taskbar (Superbar) Progress Bar he wrote a little helper because he wanted the colors to be green, yellow or red depending on the value of the CPU usage or Memory usage:

public void SetTaskBarStatus(int value)
{
if (value < 0)
{
value = 0;
}
else if (value > 100)
{
value = 100;
}

var state = TaskbarProgressBarState.Normal;

if (value > _settings.Yellow)
{
state = value < _settings.Red ? TaskbarProgressBarState.Paused : TaskbarProgressBarState.Error;
}

TaskbarManager.Instance.SetProgressState(state);
TaskbarManager.Instance.SetProgressValue(value, 100);
}

Then he just sets up a little System.Timer love and sets the Progress Bar values appropriately for Memory...

public partial class App : Application
{
private ComputerInfo _computerInfo;
private ulong _totalPhysicalMemory;

protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);

_computerInfo = new ComputerInfo();
_totalPhysicalMemory = _computerInfo.TotalPhysicalMemory;

var mainWindow = new MainWindow();
mainWindow.Tick += WhenTimerTick;
mainWindow.Show();
}

private void WhenTimerTick(object sender, EventArgs e)
{
var available = (double)(_totalPhysicalMemory-_computerInfo.AvailablePhysicalMemory) / _totalPhysicalMemory;
((MainWindow)sender).SetTaskBarStatus((int)(available * 100));
}
}

or CPU...

public partial class App : Application
{
private readonly PerformanceCounter _counter = new PerformanceCounter();

protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);

_counter.CategoryName = "Processor";
_counter.CounterName = "% Processor Time";
_counter.InstanceName = "_Total";

var mainWindow = new MainWindow();
mainWindow.Tick += WhenTimerTick;
mainWindow.Show();
}

private void WhenTimerTick(object sender, EventArgs e)
{
((MainWindow)sender).SetTaskBarStatus((int)_counter.NextValue());
}
}

Jeff also adds some JumpLists to launch Task Manager or Resource Monitor on right-click as well. Nice touch! A little polish there.

image

Also easy to do with the Windows 7 APIs in the Windows API Code Pack.

var jumpList = JumpList.CreateJumpList();
var systemFolder = Environment.GetFolderPath(Environment.SpecialFolder.System);

jumpList.AddUserTasks(new JumpListLink(Path.Combine(systemFolder, "taskmgr.exe"), "Open Task Manager")
{
IconReference = new IconReference(Path.Combine(systemFolder, "taskmgr.exe"), 0)
});

jumpList.AddUserTasks(new JumpListLink(Path.Combine(systemFolder, "perfmon.exe"), "Open Resource Monitor")
{
IconReference = new IconReference(Path.Combine(systemFolder, "perfmon.exe"), 0),
Arguments = "/res"
});

jumpList.Refresh();

Nice job, Jeff Key. You rock. So, Dear Reader, go light up YOUR applications under Windows 7. Enjoy!

Patching this Open Source Project and adding a Disk IO Meter

A day later, @ScottMuc tweeted me about adding a Disk IO Meter and we went back and forth about it on Twitter. He eventually submitted a patch to CodePlex. While Jeff hasn't updated his code with that patch (maybe he'll make me an admin and I can do it), I'm able to patch my local copy, of course.

Useful Link: Example: How to contribute a patch to an Open Source Project

Downloading ScottMuc's patch and simply right clicking (using Tortoise SVN) and clicking Apply Patch gives me a new TaskbarDiskIOMeter project that I can then add to the larger solution. The only problem with the patch was that it refers to a binary file called Drive.ico that didn't get included in the .patch file. I found one and added it and now we've got a Disk IO monitor as well. :)

 image

Enjoy!


1. Get Windows 7 and the SDK

2. Develop and Test Your Application

3. Get the Windows 7 Logo

4. Light Up Your Application with Windows 7

Related Links



Thursday, October 29, 2009 9:06:03 PM (Pacific Standard Time, UTC-08:00)
Thanks for stepping up and putting in the patch -- I didn't see it. Just like that one time when we were hiking in the Andes. The memories!

(I didn't know that patch notifications where turned off by default(!) for administrators. Fixed that too.)
Jeff Key
Thursday, October 29, 2009 10:06:21 PM (Pacific Standard Time, UTC-08:00)
Nice app, Nice Article and thanks for the patch.

Btw Scott, the link for Life Rocks is incorrect ;)
Thursday, October 29, 2009 10:20:44 PM (Pacific Standard Time, UTC-08:00)
Heh, the funny thing is that I wouldn't even bothered trying if you didn't simply give me that little nudge on twitter! Thanks for the mention.

Scott (yet another developer named Scott)
Thursday, October 29, 2009 10:39:24 PM (Pacific Standard Time, UTC-08:00)
OT: Scott, is there any place where I can give feedback / file bugs for Windows 7 RTM?
Murtaza Iqbal
Thursday, October 29, 2009 11:55:56 PM (Pacific Standard Time, UTC-08:00)
This is pretty cool and relatively clean IMO given that it's not built in. Still excited for .NET 4.0's wrapper classes.
Friday, October 30, 2009 6:53:43 AM (Pacific Standard Time, UTC-08:00)
I'd argue that this is exactly the sort of thing that shouldn't be on the taskbar ;)

http://msdn.microsoft.com/en-us/library/aa511446.aspx

MS shouldn't have hidden away the notification area in 7 - it was useful for this sort of thing, e.g. a minimized Task Manager instance.

Still, a good example of coding for Win7 in .NET.
Dave R.
Friday, October 30, 2009 10:00:32 AM (Pacific Standard Time, UTC-08:00)
Don't you think you should have hidden his email address in that pic? Jeff might find himself with A LOT more "friends" on monday. ;)
OpenID
Please login with either your OpenID above, or your details below.
Name
E-mail
(will show your gravatar icon)
Home page

Comment (Some html is allowed: a@href@title, b, blockquote@cite, em, i, pre, strike, strong, sub, super, u) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Live Comment Preview

Contact

Sponsors

Hosting By

Hot Topics

Tags

Calendar

<November 2009>
SunMonTueWedThuFriSat
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345

Archives

November, 2009 (5)
October, 2009 (19)
September, 2009 (11)
August, 2009 (12)
July, 2009 (21)
June, 2009 (26)
May, 2009 (16)
April, 2009 (13)
March, 2009 (17)
February, 2009 (17)
January, 2009 (18)
December, 2008 (32)
November, 2008 (17)
October, 2008 (22)
September, 2008 (16)
August, 2008 (14)
July, 2008 (25)
June, 2008 (19)
May, 2008 (17)
April, 2008 (17)
March, 2008 (26)
February, 2008 (21)
January, 2008 (28)
December, 2007 (19)
November, 2007 (17)
October, 2007 (31)
September, 2007 (39)
August, 2007 (37)
July, 2007 (43)
June, 2007 (37)
May, 2007 (32)
April, 2007 (38)
March, 2007 (29)
February, 2007 (46)
January, 2007 (31)
December, 2006 (27)
November, 2006 (31)
October, 2006 (32)
September, 2006 (39)
August, 2006 (34)
July, 2006 (40)
June, 2006 (18)
May, 2006 (31)
April, 2006 (34)
March, 2006 (30)
February, 2006 (38)
January, 2006 (44)
December, 2005 (19)
November, 2005 (34)
October, 2005 (24)
September, 2005 (37)
August, 2005 (20)
July, 2005 (24)
June, 2005 (33)
May, 2005 (16)
April, 2005 (22)
March, 2005 (34)
February, 2005 (15)
January, 2005 (37)
December, 2004 (28)
November, 2004 (30)
October, 2004 (34)
September, 2004 (22)
August, 2004 (34)
July, 2004 (18)
June, 2004 (64)
May, 2004 (49)
April, 2004 (21)
March, 2004 (29)
February, 2004 (29)
January, 2004 (36)
December, 2003 (25)
November, 2003 (24)
October, 2003 (59)
September, 2003 (42)
August, 2003 (24)
July, 2003 (44)
June, 2003 (29)
May, 2003 (21)
April, 2003 (30)
March, 2003 (27)
February, 2003 (47)
January, 2003 (50)
December, 2002 (31)
November, 2002 (38)
October, 2002 (44)
September, 2002 (15)
May, 2002 (2)
April, 2002 (4)

Google Ads