Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you show progress in the Taskbar with Winform C# 4.5 [duplicate]

EDIT: I don't want it to update, change or go away. I ONLY want the taskbar to be at 40% to start the program, stay that way till it closes.

I spent a lot of hours and tried many examples...but no luck.

To keep it simple, how do you show 40% done in Error color?

This code runs but does nothing on the screen, no errors, just runs right by:

public TaskbarItemInfo taskBar = new TaskbarItemInfo();

then in a method:

taskBar.ProgressState = System.Windows.Shell.TaskbarItemProgressState.Error;
taskBar.ProgressValue = 0.40;

If you breakpoint on the next line and look, it has set the values, they just don't do anything on the screen...

like image 344
Sparfy Avatar asked Feb 08 '15 06:02

Sparfy


People also ask

How do I use the ProgressBar in Winforms?

To create a ProgressBar control at design-time, you simply drag a ProgressBar control from the Toolbox and drop onto a Form in Visual Studio. After you the drag and drop, a ProgressBar is created on the Form; for example the ProgressBar1 is added to the form and looks as in Figure 1.

What is ProgressBar in Windows form?

A progress bar is a control that an application can use to indicate the progress of a lengthy operation such as calculating a complex result, downloading a large file from the Web etc. ProgressBar controls are used whenever an operation takes more than a short period of time.


2 Answers

TaskbarItemInfo doesn't do anything by itself. It needs a window which is represented on the taskbar. Note that one normally gets an instance of TaskbarItemInfo from an instance of a WPF Window. I.e. that class is intended for use in WPF programs, not Winforms.

For a Winforms program, you may find it is more practical to use the Windows API Codepack, which if I recall correctly has support for this Shell feature.

You can use the TaskbarManager class in WindowsAPICodePack.Taskbar to set the Form Window's task bar progress like this:

using Microsoft.WindowsAPICodePack.Taskbar;
...
private void Form1_Load(object sender, EventArgs e)
{
    TaskbarManager.Instance.SetProgressState(TaskbarProgressBarState.Error, Handle);
    TaskbarManager.Instance.SetProgressValue(40, 100, Handle);
}

Using the current Form's .Handle to tell the manager to which window this feature should be provided. You can use a public static pointer reference from another form, too, if you wish to handle its progress in the same place.

Unfortunately, for some reason Microsoft is no longer hosting a download for this, in spite of the continued relevance for the library. But here is a StackOverflow Q&A with numerous other links for the same library: Windows API Code Pack: Where is it?. Note that there are two versions, 1.0 and 1.1. In general, you will likely prefer the 1.1 version; it has numerous bug fixes, added features, and much better Fxcop compliance. The link I've provided is for 1.1, but there are links for downloading 1.0 on that SO article as well.

like image 146
Peter Duniho Avatar answered Oct 06 '22 00:10

Peter Duniho


Here's a short example that you should be able to use to tailor to your needs:

    System.Windows.Window w = new System.Windows.Window();
    w.TaskbarItemInfo = new System.Windows.Shell.TaskbarItemInfo() { ProgressState = System.Windows.Shell.TaskbarItemProgressState.Normal };
    w.Loaded += delegate {
        Action<Object> callUpdateProgress = (o) => {
            w.TaskbarItemInfo.ProgressValue = (double) o;
        };

        Thread t = new Thread(() => {
            for (int i = 1; i <= 10; i++) {
                w.Dispatcher.BeginInvoke(callUpdateProgress, 1.0 * i / 10);
                Thread.Sleep(1000);
            }
        });
        t.Start();
    };

    System.Windows.Application app = new System.Windows.Application();
    app.Run(w);

To make the above work you need to have using System.Threading; at top and also add references of: PresentationCore, PresentationFramework, SystemXaml, WindowsBase.

like image 42
Loathing Avatar answered Oct 05 '22 23:10

Loathing