AdSense

Monday, 2 September 2013

C# WPF - Show progress in task bar

(Deutsche Version) Downloading something with firefox will show the progress in the task bar. Realising this is pretty simple. In the window you want to have this, you have to add

<Window.TaskbarItemInfo>
    <TaskbarItemInfo/>
</Window.TaskbarItemInfo>


just in front of the <Grid>. Now you can access TaskbarItemInfo. This could be done like this:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();


        TaskbarItemInfo.ProgressState = System.Windows.Shell.TaskbarItemProgressState.Normal;
 
        Loaded += MainWindow_Loaded;
    }

    void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        TaskbarItemInfo.ProgressValue = 0.5;

    }
}


You can use different ProgressStates so the progress bar changes its color. You can also define a symbol for the task bar.

If you want to do all these things from another thread, this will not work. Why is explained in this post. I will only show the solution. Instead of

TaskbarItemInfo.ProgressValue = 0.2

you have to use

Application.Current.Dispatcher.BeginInvoke(new Action(() =>
{
    TaskbarItemInfo.ProgressValue = 0.2;
}));

No comments:

Post a Comment