Esempio n. 1
0
        private void WebClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            string received  = string.Format(CultureInfo.InvariantCulture, "{0:n0} kB", e.BytesReceived / 1000);
            string toReceive = string.Format(CultureInfo.InvariantCulture, "{0:n0} kB", e.TotalBytesToReceive / 1000);

            if (e.BytesReceived / 1000000 >= 1)
            {
                received = string.Format("{0:.#0} MB", Math.Round((decimal)e.BytesReceived / 1000000, 2));
            }
            if (e.TotalBytesToReceive / 1000000 >= 1)
            {
                toReceive = string.Format("{0:.#0} MB", Math.Round((decimal)e.TotalBytesToReceive / 1000000, 2));
            }

            TimeSpan timeSpent        = DateTime.Now - updateStartTime;
            int      secondsRemaining = (int)(timeSpent.TotalSeconds / e.ProgressPercentage * (100 - e.ProgressPercentage));
            TimeSpan timeLeft         = new TimeSpan(0, 0, secondsRemaining);
            string   timeLeftString   = string.Empty;
            string   timeSpentString  = string.Empty;

            if (timeLeft.Hours > 0)
            {
                timeLeftString += string.Format("{0} hours", timeLeft.Hours);
            }
            if (timeLeft.Minutes > 0)
            {
                timeLeftString += string.IsNullOrWhiteSpace(timeLeftString) ? string.Format("{0} min", timeLeft.Minutes) : string.Format(" {0} min", timeLeft.Minutes);
            }
            if (timeLeft.Seconds >= 0)
            {
                timeLeftString += string.IsNullOrWhiteSpace(timeLeftString) ? string.Format("{0} sec", timeLeft.Seconds) : string.Format(" {0} sec", timeLeft.Seconds);
            }

            if (timeSpent.Hours > 0)
            {
                timeSpentString = string.Format("{0} hours", timeSpent.Hours);
            }
            if (timeSpent.Minutes > 0)
            {
                timeSpentString += string.IsNullOrWhiteSpace(timeSpentString) ? string.Format("{0} min", timeSpent.Minutes) : string.Format(" {0} min", timeSpent.Minutes);
            }
            if (timeSpent.Seconds >= 0)
            {
                timeSpentString += string.IsNullOrWhiteSpace(timeSpentString) ? string.Format("{0} sec", timeSpent.Seconds) : string.Format(" {0} sec", timeSpent.Seconds);
            }

            DownloadingProgressed?.Invoke(this, new DownloadProgressEventArgs(e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage, timeLeftString, timeSpentString, received, toReceive));
        }
Esempio n. 2
0
 /// <summary>
 /// Calls the DownloadProgressed event
 /// </summary>
 /// <param name="sender">Sender of the event</param>
 /// <param name="args">Args to be passed to the event</param>
 void DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs args)
 {
     DownloadingProgressed?.Invoke(this, new DownloadProgressEventArgs(args.ProgressPercentage, args.BytesReceived, args.TotalBytesToReceive));
 }