Did you ever try to download a file from the Internet with Windows 7? If yes, maybe you noticed that the Internet Explorer icon in the task bar shows the download progress, like a classic ProgressBar. With WPF 4 and VB (o C#) 2010 it's possible programming this interesting functionality in order to make our applications able of showing the progress state of a given task. The code example shown in this blog post simply wastes some time in an asynchronous way so that it can send progress state to a ProgressBar control and to the icon in the task bar (the reason about this is that the focus is understanding how the progress bar works, not providing complex code). For the sake of completeness, I took some tips from this blog post by Pete Brown, that I revisited in a VB fashion.
On the XAML side, first we need the usual reference to the task bar, then we can add a ProgressBar inside the Window:
<Window.TaskbarItemInfo>
<TaskbarItemInfo/>
</Window.TaskbarItemInfo>
<Grid>
<ProgressBar Name="ProgressBar1" Height="40" Margin="5"/>
</Grid>
Next, let's write some VB code which makes use of a
BackgroundWorker control to make async operations, whose progress will be reported on both the task bar and on the ProgressBar. The following code demonstrates how wasting your time and how you can send the progress to the task bar at the same time:
Class MainWindow
Private Sub MainWindow_Loaded(ByVal sender As Object,
ByVal e As RoutedEventArgs) Handles Me.Loaded
Dim worker As New System.ComponentModel.BackgroundWorker
AddHandler worker.DoWork, AddressOf WasteSomeTime
AddHandler worker.ProgressChanged, AddressOf worker_ProgressChanged
AddHandler worker.RunWorkerCompleted, AddressOf worker_RunWorkerCompleted
worker.WorkerReportsProgress = True
'Set the progress state as "normal"
TaskbarItemInfo.ProgressState = Shell.TaskbarItemProgressState.Normal
'Start the work
worker.RunWorkerAsync()
End Sub
'Waste some time, simulating some work
Private Sub WasteSomeTime(ByVal sender As Object,
ByVal e As System.ComponentModel.DoWorkEventArgs)
For i As Integer = 0 To 99 Step 10
System.Threading.Thread.Sleep(1000)
'Raises the ProgressChanged event passing the value
CType(sender, System.ComponentModel.BackgroundWorker).ReportProgress(i)
Next i
End Sub
'Finally set values for controls
Private Sub worker_RunWorkerCompleted(ByVal sender As Object,
ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs)
ProgressBar1.Value = 100
TaskbarItemInfo.ProgressValue = 1.0
End Sub
'Increment the control's progress value
Private Sub worker_ProgressChanged(ByVal sender As Object,
ByVal e As System.ComponentModel.ProgressChangedEventArgs)
ProgressBar1.Value = e.ProgressPercentage
TaskbarItemInfo.ProgressValue = CDbl(e.ProgressPercentage) / 100
End Sub
End Class
The following are the most two interesting lines of code from all the one shown above. The first line:
'Set the progress state as "normal"
TaskbarItemInfo.ProgressState = Shell.TaskbarItemProgressState.Normal
Here the code tells the icon on the task bar that the progress bar will have a normal aspect, which is green. Other choices are available from the Shell.TaskBarItemProgressState enumeration, which allow to customize the progress bar aspect. For instance, Error will make the progress state's color as red, Paused as yellow, while Indeterminate will represent the progress state as a loop; this is typical when you want to represent an idle state or a freezed task.
This is instead the second interesting line:
TaskbarItemInfo.ProgressValue = 1.0
Here we just assign to the ProgressValue property the value that represents the progress state. This is a moment of what we get on the task bar:
By the way, such feature consumes some more system resources so you should use it carefully. The source code accompanying this blog post is available from the MSDN Code Gallery here.
Finally, I'd like to mention that there are lots of other ways (and resources) for programming Windows 7 with managed code, not necessarily related to WPF 4. If you are interested in this kind of development, have a look at the Windows API Code Pack which provides managed access to lots of features of the new operating system and that can be also used with Visual Studio 2008.
Alessandro