How do you show progress on the taskbar with Winform C # 4.5

EDIT: I don't want it to update, change, or go. I ONLY want the taskbar to be at 40% to run the program, stay there until it closes.

I spent many hours and tried many examples ... but no luck.

To make it simple, how do you show 40% in color error?

This code works, but does nothing on the screen, no errors, it just executes directly:

public TaskbarItemInfo taskBar = new TaskbarItemInfo(); 

then in the method:

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

If you leave a dot on the next line and look, it has set the values, they just do not do anything on the screen ...

+3
source share
3 answers

Here is a quick example that you can use to adapt 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); 
+2
source

TaskbarItemInfo does nothing on its own. He needs a window that is presented on the taskbar. Note that it usually gets an instance of TaskbarItemInfo from a WPF Window instance. That is, this class is intended for use in WPF programs, not Winforms.

For Winforms, you may find it more practical to use the Windows API Codepack , which, if I remember correctly, supports this Shell function.

You can use the TaskbarManager class in WindowsAPICodePack.Taskbar to customize the form window's taskbar 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 .Handle form to tell the manager which window this function should be provided in. You can use the public static link pointer from another form if you want to cope with its progress in the same place.

Unfortunately, for some reason, Microsoft is no longer downloading for this, despite the ongoing relevance to the library. But here is StackOverflow Q & A with many other links for the same library: Windows API code: where is it? . Please note that there are two versions: 1.0 and 1.1. In general, you will most likely prefer version 1.1; it has numerous bug fixes, added features, and much better Fxcop compliance. The link I provided has a value of 1.1, but there are links for downloading 1.0 in this SO article.

+4
source

I did this by creating a separate thread from the main frame thread to execute code that sends a run update where you can call setProgress in the progress bar, but you must create a delegation method, otherwise you will get an exception that causes your thread to access the element controls in the main thread, that’s what I would do,

declare a delegate method in your class, if you have a progress bar,

 public delegate void SetProgressDelg(int level); 

Then implement this method to update the progress bar,

 public void SetProgress(int level) { if (this.InvokeRequired) { SetProgressDelg dlg = new SetProgressDelg(this.SetProgress); this.Invoke(dlg, level); return; } progressBar.Value = level; } 

hope this works, I use this in several applications and it works great.

This is how you build a progress bar,

  ToolStripContainer = toolStripContainer1 = new System.Windows.Forms.ToolStripContainer(); // StatusBar // ToolStripStatusLabel StatusBar = new System.Windows.Forms.ToolStripStatusLabel(); StatusBar.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text; StatusBar.ForeColor = System.Drawing.Color.Blue; StatusBar.LinkColor = System.Drawing.Color.Navy; StatusBar.Name = "StatusBar"; StatusBar.Size = new System.Drawing.Size(732, 20); StatusBar.Spring = true; StatusBar.Text = "Status Messages Go Here"; StatusBar.TextAlign = System.Drawing.ContentAlignment.MiddleLeft; // // ProgressBar // ToolStripProgressBar ProgressBar = new System.Windows.Forms.ToolStripProgressBar(); ProgressBar.ForeColor = System.Drawing.Color.Yellow; ProgressBar.Name = "ProgressBar"; ProgressBar.Size = new System.Drawing.Size(150, 19); // // StatusStrip // StatusStrip StatusStrip = new System.Windows.Forms.StatusStrip(); StatusStrip.Dock = System.Windows.Forms.DockStyle.None; StatusStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { StatusBar, this.ProgressBar}); StatusStrip.Location = new System.Drawing.Point(0, 0); StatusStrip.Name = "StatusStrip"; StatusStrip.Size = new System.Drawing.Size(899, 25); StatusStrip.TabIndex = 0; toolStripContainer1.BottomToolStripPanel.Controls.Add(this.StatusStrip); 

then you want to add toolStripContainer to the controls of the main panel.

you want to call SetProgress from a thread that processes your task, this is how you start a thread,

  //* from your class of your main frame //* this is where SetStatus is defined //* start a thread to process whatever task //* is being done Thread t = new Thread(StartProc); t.Start(); public void StartProc() { //* start processing something, //*let assume your are processing a bunch of files List<string> fileNames; for (int i = 0; i < fileNames.Count; i++) { //* process file here //* ........... //* then update progress bar SetProgress((int)((i + 1) * 100 / fileNames.Length)); } //* thread will exit here } 

Let me know if you need anything else, hope this helps,

+1
source

All Articles