Refresh ProgressBar in another window from function?

I have a background task that downloads a file, and I want it to report progress to the progress panel in a different form. How can i do this? I am a little new to C #, but a long time VB.net programmer.

I was messing around with this code, but its completely wrong.

System.Windows.Forms.Form progForm = new ProgressWindows(); Control[] ctrls = progForm.Controls.Find("fileProgress1", true); 
+4
source share
3 answers

If the problem is related to the Progress1 file. The easiest solution is to make it public in the ProgressWindow class. By default, controls are private.

enter image description here

You can then access the fileProgress1 control as shown below.

 ProgressWindows progForm = new ProgressWindows(); //progForm.fileProgress1 

However, the best way is to open a public method in the PrefressWindows class, which updates the move.

In class ProgressWindows

 public void UpdateProgressBar(int percentage) { // Set the progress in the progress bar. fileProgress1.Percentage = percentage; } 

Call the above method as shown below.

 ProgressWindows progForm = new ProgressWindows(); progForm.UpdateProgressBar(percentage); 
+1
source

If you are using BackgroundWorker , just call ReportProgress . Otherwise, you will need to send the user interface change to the desired stream. In WinForms, see the Control.InvokeRequired Property and its associated methods. Equivalent to WPF DispatcherObject.VerifyAccess .

Edit: Visual Studio is not in front of me, so there may be some minor compilation errors.

 public partial class MyForm : Form { public MyForm() { InitializeComponent(); // fileProgress1 setup. } private void StartTask() { Task t1 = new Task(BackgroundMethod1, fileProgress1); // Explicitly pass a reference to the progress bar. Task t2 = new Task(BackgroundMethod2); // Use a method that has access to the bar. Task t5 = new Task(BackgroundMethod3, IncrementPBMethod); // Pass an action to the background method. Abstracting the physical progress bar as something where you can set the progress. Task t4 = new Task(delegate() { /* fileProgress1 referened*/ }); // Create a closure. I don't recommend this method. } private static void BackgroundMethod1(ProgressBar pb) { for(int i = 0; i < 100; ++i) { if(pb.InvokeRequired) { pb.Invoke(delegate() { pb.Value = i; }); } Thread.Sleep(1000); } } private void BackgroundMethod2() { for(int i = 0; i < 100; ++i) { if(fileProgress1.InvokeRequired) { fileProgress1.Invoke(delegate() { fileProgress1.Value = i; }); } Thread.Sleep(1000); } } private static BackgroundMethod3(Action<int> setProgress) { for(int i = 0; i < 100; ++i) { setProgress(i); Thread.Sleep(1000); } } private void IncrementPBMethod(int value) { if(fileProgress1.InvokeRequired) { fileProgress1.Invoke(IncrementPBMethod, value); } else { fileProgress1.Value = value; } } } 
+2
source

I assume that your second form (the one you want to show progress) is created in the form that performs the "task". Why don't you just create a public method in the second form, except for an integer parameter to update the progress bar value? This seems like a relatively simple solution.

0
source

All Articles