Windows API Code Pack – Progress Bar

14. September 2009

Yesterday, I wrote about the Windows API Code Pack, and how you can add icon overlay support to your application. Another feature that the API gives you is the ability to add a progress bar to the taskbar. It is similar to icon overlays in that it changes the appearance of your icon, but only slightly. Instead of overlaying something on top of the application icon, it turns the background into a progress bar.

using Microsoft.WindowsAPICodePack.Taskbar; 
private void LongRunningProcess() 
{ 
    TaskbarManager.Instance.SetProgressState(TaskbarProgressBarState.Normal); 
    for (int i = 0; i < 100; i++) 
    { 
        TaskbarManager.Instance.SetProgressValue(i, 99); 
        Thread.Sleep(100); 
    } 
    TaskbarManager.Instance.SetProgressState(TaskbarProgressBarState.NoProgress); 
} 

There is support for other states your application could be in as well.
TaskbarProgressBarState.Indeterminate – puts the progress bar in “marquee” mode
TaskbarProgressBarState.Paused – causes the background to glow yellow
TaskbarProgressBarState.Error – causes the background to glow red


short link to this post:

.net, windows 7 ,

Windows API Code Pack – Icon Overlays

13. September 2009

Earlier this month v1.0 of the Windows API Code Pack was released. The API allows you to do things that are not currently available today. You can check out the site for a complete list of available features, but the one I am looking at right now is icon overlays.

Essentially, with icon overlays, you can change how your icon looks in the taskbar based on certain conditions in your application. For example, a virus scanner could overlay something over the icon to let you know when a scheduled scan is happening. A backup application could change the icon based the last time a backup was run. The use is really up to you, but for now I just wanted to play - nothing fancy just a checkbox that will overlay a green orb when checked, and remove it when unchecked.

Just to be clear we are not swapping two icons in and out. The first icon is the blue application icon, and the second is a green orb. The API will actually overlay the second one on the first (in the bottom right corner). It also provides some nice fading animation as well.

using Microsoft.WindowsAPICodePack.Taskbar; 
if (OverlayCheckbox.Checked) 
{ 
    Icon greenOrb = Properties.Resources.GreenButton; 
    TaskbarManager.Instance.SetOverlayIcon(greenOrb, "Some Status"); 
} 
else 
{ 
    TaskbarManager.Instance.SetOverlayIcon(null, ""); 
} 

iconOverlay


short link to this post:

.net, windows 7 ,

Parallel Programming in .net 4.0

10. September 2009

One of the new features added to the .net 4 framework is support for parallel computing. Per MSDN:

The manycore shift presents an unprecedented business opportunity for developers to design new software experiences that take advantage of the performance power of manycore architectures. At the same time, parallel programming is complex, difficult and labor-intensive, for even the most skilled developers….

Microsoft’s goal is to increase productivity by encapsulating complexity, so developers can focus on solving business problems.

I’m not an expert in this subject (and I don’t pretend to be!), but I had some spare time the other day and decided to play around a bit.

The test was simple - create a new GUID, and hash it using the MD5 algorithm. Repeat this 30,000,000 times. First up was a standard for loop.

for (int i = 0; i < 30000000; i++) 
{ 
    using (MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider()) 
    {  
        string value = Guid.NewGuid().ToString(); 
        md5.ComputeHash(UTF8Encoding.UTF8.GetBytes(value)); 
    } 
} 

It took 4 minutes, 58 seconds and if you look at the CPU usage, you can see only 1 core was used. Next up was the parallel  for loop:

Parallel.For(0, 30000000, delegate(int i) 
{ 
    using (MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider()) 
    { 
        string value = Guid.NewGuid().ToString(); 
        md5.ComputeHash(UTF8Encoding.UTF8.GetBytes(value)); 
    } 
}); 

This time around 30 million GUIDs were created and hashed in 1 minute, 17 seconds. Taking a look at the CPU usage, we can see that the framework automatically farmed the work out across every core.

I haven’t looked into how well the Parallel FX handles thread safety, or how well it performs under more realistic scenarios; but so far I am impressed with what I see. Yet another great move coming out of the .net team!

Resources:
Parallel Computing Developer Center
Parallel Programming in the .NET Framework
Optimize Managed Code For Multi-Core Machines
Running Queries On Multi-Core Processors
PFX Team Blog


short link to this post:

.net ,