0
votes
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Threading;

namespace SatelliteImages
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            ExtractImages ei = new ExtractImages();
            ei.Init();

            progressBar1.Minimum = 0;
            progressBar1.Maximum = 100;

            backgroundWorker1.RunWorkerAsync();
        }



        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;

            for (int i = 0; i <= ExtractImages.imagesUrls.Count(); i++)
            {
                if (worker.CancellationPending == true)
                {
                    e.Cancel = true;
                    break;
                }
                else
                {
                    using (var client = new WebClient())
                    {
                        client.DownloadFile(ExtractImages.imagesUrls[i], @"C:\Temp\TestingSatelliteImagesDownload\" + i + ".jpg");
                        worker.ReportProgress(100 * i / ExtractImages.imagesUrls.Count());
                    }
                }
            }
        }

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            progressBar1.Value = e.ProgressPercentage;
            label2.Text = (e.ProgressPercentage.ToString() + "%");
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                MessageBox.Show("Completed without any errors");
            }
            else
            {
                string myerr = e.Error.ToString();
            }
        }
    }
}
  1. Why the progressBar1 is getting to 99% and stop there and then getting to the completed event? How can i make it to get to 100% ?

  2. Is the way i'm calculating the reportProgress and the FOR loop starting from i = 0 is fine ?

  3. How can i display on the form a new label for example label3 and show the number of files left to download ? For example if in imagesUrls there are 399 items( files to download ) then in label3 i want to see a counter from 399 to 0 or to 1 each time a file was downloaded completed and was seccessed.

  4. When it's getting now to the completed event i'm getting exception:

e.Error = {"Index was out of range. Must be non-negative and less than the size of the collection.\r\nParameter name: index"}

at System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource) at System.Collections.Generic.List`1.get_Item(Int32 index) at SatelliteImages.Form1.backgroundWorker1_DoWork(Object sender, DoWorkEventArgs e) in D:\C-Sharp\SatelliteImages\SatelliteImages\SatelliteImages\Form1.cs:line 60 at System.ComponentModel.BackgroundWorker.OnDoWork(DoWorkEventArgs e)
at System.ComponentModel.BackgroundWorker.WorkerThreadStart(Object argument)

Update of what i tried:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Threading;

namespace SatelliteImages
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            ExtractImages ei = new ExtractImages();
            ei.Init();
        }



        private async void Form1_Load(object sender, EventArgs e)
        {
            label1.Text = "0 %";
            progressBar1.Minimum = 0;
            progressBar1.Maximum = ExtractImages.imagesUrls.Count() - 1;
            await DoWork();
        }

        private async Task DoWork()
        {
            for (int i = 0; i < ExtractImages.imagesUrls.Count(); i++)
            {
                using (var client = new WebClient())
                {
                    await client.DownloadFileTaskAsync(ExtractImages.imagesUrls[i], @"C:\Temp\TestingSatelliteImagesDownload\" + i + ".jpg");

                    progressBar1.Value = i + 1;
                    double average = (double)(i + 1) / ExtractImages.imagesUrls.Count();
                    label1.Text = (Math.Round(average, 1) * 100).ToString() + " %";
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }
    }
}
  1. The progressBar is not getting to the end it stop on 99% i think it's 99%.

  2. The label1 show 100% in the end but the progressBar is not getting to the end.

  3. In the end i'm also getting exception:

The exception is in Program.cs on the line:

Application.Run(new Form1());

Additional information: Exception has been thrown by the target of an invocation.

System.Reflection.TargetInvocationException was unhandled
  HResult=-2146232828
  Message=Exception has been thrown by the target of an invocation.
  Source=mscorlib
  StackTrace:
       at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
       at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
       at System.Delegate.DynamicInvokeImpl(Object[] args)
       at System.Windows.Forms.Control.InvokeMarshaledCallbackDo(ThreadMethodEntry tme)
       at System.Windows.Forms.Control.InvokeMarshaledCallbackHelper(Object obj)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Windows.Forms.Control.InvokeMarshaledCallback(ThreadMethodEntry tme)
       at System.Windows.Forms.Control.InvokeMarshaledCallbacks()
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(Form mainForm)
       at SatelliteImages.Program.Main() in D:\C-Sharp\SatelliteImages\SatelliteImages\SatelliteImages\Program.cs:line 19
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 
       HResult=-2146233086
       Message=Value of '399' is not valid for 'Value'. 'Value' should be between 'minimum' and 'maximum'.
Parameter name: Value
       ParamName=Value
       Source=System.Windows.Forms
       StackTrace:
            at System.Windows.Forms.ProgressBar.set_Value(Int32 value)
            at SatelliteImages.Form1.<DoWork>d__2.MoveNext() in D:\C-Sharp\SatelliteImages\SatelliteImages\SatelliteImages\Form1.cs:line 46
         --- End of stack trace from previous location where exception was thrown ---
            at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
            at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
            at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
            at SatelliteImages.Form1.<Form1_Load>d__1.MoveNext() in D:\C-Sharp\SatelliteImages\SatelliteImages\SatelliteImages\Form1.cs:line 35
         --- End of stack trace from previous location where exception was thrown ---
            at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<>c.<ThrowAsync>b__6_0(Object state)
       InnerException: 
2
Why don't you just have the progressBar1.Maximum = ExtractImages.imagesUrls.Count() and avoid all these calculations. And yes starting from 0 is correct. What version of .NET framework are you targeting ? 4.5 and higher ?Zein Makki
@user3185569 i'm using as target in this project .net 4.5.2Moses Meteor
@user3185569 look at my updated question with the Update part please i also commented on your solution. Thank you.Moses Meteor
@MosesMeteor Please change the maximum value to ExtractImages.imagesUrls.Count(). You don't need to subtract 1 from it. Then your code should work. Please try and update on the result.Rohit Dodle

2 Answers

1
votes

To fix your answer, you need to replace <= with < in the for loop.

Now you can see why the async-await pattern is useful, the code flows as any normal code. Check the below simple program the does the same as yours:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private async void Form1_Load(Object sender, EventArgs e)
    {
        label1.Text = "0 %";
        progressBar1.Minimum = 0;
        progressBar1.Maximum = ExtractImages.imagesUrls.Count() - 1;
        await DoWork();
    }

    private async Task DoWork()
    {
        for (int i = 0; i < ExtractImages.imagesUrls.Count(); i++)
        {
            using (var client = new WebClient())
            {
                await client.DownloadFileTaskAsync(ExtractImages.imagesUrls[i], @"C:\Temp\TestingSatelliteImagesDownload\" + i + ".jpg");

                progressBar1.Value = i + 1;
                double average = (double)(i + 1) / ExtractImages.imagesUrls.Count();
                label1.Text = (Math.Round(average, 1) * 100).ToString() + " %";
            }
        }
    }
}

Note that you can use IProgress<int> and decouple the code that updates the UI from the code that downloads the file:

private async void Form1_Load(Object sender, EventArgs e)
{
    label1.Text = "0 %";
    var progress = new Progress<int>();
    progress.ProgressChanged += Progress_ProgressChanged;
    await DoWork(progress);
}

private void Progress_ProgressChanged(Object sender, Int32 e)
{
     progressBar1.Value = e + 1;
     double average = ((double)(e + 1) / (ExtractImages.imagesUrls.Count()));
     label3.Text = (Math.Round(average, 1) * 100).ToString() + " %";
}

Then you add after the download file:

progress.Report(i);
1
votes

For your 1st, 2nd and 4th questions:

You are looping from 0 to count inclusive (0 to <=count) on either sides. It means, you are looping count+1 times. I do not suppose that is your intention. To fix it, I suggest you to start with i=1. I advice against going with i=0 and i lessthan count because the progressbar will not be added any progress for i=0 as per your calculation. Though these suggestions may help, I strongly recommend you to set progressbar.Maximum value to count and just increment progressbar.Value by one after each file download.

I suspect your calculation is rounding of the result of division to integer. So, first multiply i with 100.0 and then perform division. Like (i*100.0)/(images_count).

For your 3rd requirement:

Please refer the MSDN example- https://msdn.microsoft.com/en-us/library/t9fzsyec(v=vs.110).aspx