private void updateprogressforitem(ListViewItem UpdateItem, DownloadProgressChangedEventArgs args) { //updates the progress for one of the items. uses the DrawItemData found in the tag of the stored BCUpdate.UpdateInfo which itself is the tag of the given item. float percentage = (float)args.BytesReceived / (float)args.TotalBytesToReceive; //long bytespeed = args.BytesReceived - lastBytesreceived; //UpdateItem.ProgressEvent = args; UpdateItem.SubItems[lvwUpdates.Columns["PROGRESS"].Index].Text = percentage.ToString(); UpdateItem.SubItems[3].Text = percentage.ToString(); DrawItemData itemdraw = ((DrawItemData)((BCUpdate.UpdateInfo)UpdateItem.Tag).Tag); long currspeed = args.BytesReceived - lastBytesreceived; if (currspeed > 0) { itemdraw.ByteSpeed = currspeed; } lastBytesreceived = args.BytesReceived; long speedpersecond = interpolatepersecond(DateTime.Now - lastprogress, itemdraw.ByteSpeed); String speedshow = ByteSizeFormatter.FormatSize(speedpersecond); if (0 < percentage && percentage < 100) { itemdraw.PercentComplete = percentage; itemdraw.StringDraw = String.Format("{0}% ({1}/s)", percentage, speedshow); } else if (args.BytesReceived == args.TotalBytesToReceive) { //complete if (itemdraw.DownloadError == null) { itemdraw.StringDraw = "Ready to Install"; itemdraw.PercentComplete = 100; } else { itemdraw.StringDraw = "Error"; UpdateItem.ToolTipText = "Error:" + itemdraw.DownloadError.Message; } } }
private void progressroutine(BCUpdate.UpdateInfo updateobject, DownloadProgressChangedEventArgs args) { if (mUpdateMode == EUpdateMode.Update_Full) { ListViewItem upobj = lookupinfo[updateobject]; updateprogressforitem(upobj, args); //we want to invalidate the areas we want to repaint. lvwUpdates.Invalidate(upobj.SubItems[3].Bounds); lvwUpdates.Update(); //calculate the average % complete of all items. float percentshow = getAverageCompletion(); String usetitle = String.Format("{0:0.0}% of {1} Items", percentshow, updatingItems.Count); Text = usetitle; if (percentshow > 0) { if (isWin7()) { Microsoft.WindowsAPICodePack.Taskbar.TaskbarManager.Instance. SetProgressValue((int)(percentshow), 100); } } } else { TimeSpan interval = DateTime.Now - lastprogress; long currentspeed = args.BytesReceived - prevDownloadAmount; //immediate if ((DateTime.Now - lastprogress).TotalSeconds >= 1) { numperiods++; if (numperiods == 5) { numperiods = 1; } } float percentcomplete = (float)(args.BytesReceived) / (float)(args.TotalBytesToReceive); float percentshow = Math.Min(percentcomplete * 100, 100); if (isWin7()) { Microsoft.WindowsAPICodePack.Taskbar.TaskbarManager.Instance. SetProgressValue((int)(percentshow), 100); } Debug.Print("Received " + args.BytesReceived.ToString() + " of " + args.TotalBytesToReceive.ToString() + "(" + percentcomplete + "%" + ")"); lblImmAction.Text = "Downloading" + CreateString('.', numperiods); //place the two sizes in an array... long[] sizes = new long[] { args.BytesReceived, args.TotalBytesToReceive }; long persecondspeed = interpolatepersecond(interval, currentspeed); String scurrentspeed = persecondspeed > 0?ByteSizeFormatter.FormatSize(persecondspeed):"(Please wait...)"; //call the formatting routine.. String[] formattedsizes = ByteSizeFormatter.FormatSizes(sizes).ToArray(); lblImmProgress.Text = String.Format("{0} of {1} ({2:0.0}%)", formattedsizes[0], formattedsizes[1], percentshow); if (lastaveragebyterate == -1) { lastaveragebyterate = persecondspeed; } long avgspeed = lastaveragebyterate / 2 + persecondspeed / 2; AllSamples.Add(avgspeed); avgspeed = (int)(AllSamples.Average()); lblRemaining.Text = FormatTimeSpan(PredictETA(avgspeed, sizes[0], sizes[1])); lblDlRate.Text = ByteSizeFormatter.FormatSize(avgspeed) + "/s"; Text = String.Format("Updating...{0:0.0}%", percentshow); prevDownloadAmount = args.BytesReceived; pbarImmediate.Value = (int)(percentshow); pbarImmediate.Invoke((MethodInvoker)(() => { pbarImmediate.Invalidate(); pbarImmediate.Update(); })); Thread.Sleep(10); } }
private void frmUpdates_Load(object sender, EventArgs e) { try { Cursor.Current = Cursors.WaitCursor; Debug.Print("frmUpdates Loaded"); switch (mUpdateMode) { case EUpdateMode.Update_Full: grpBasicUpdate.Visible = false; tryagain: updateobj = new BCUpdate(); lvwUpdates.Items.Clear(); lvwUpdates.Columns.Clear(); lvwUpdates.Columns.Add("NAME", "Name"); lvwUpdates.Columns.Add("INSTALLEDVER", "Installed"); lvwUpdates.Columns.Add("VERSION", "Version"); lvwUpdates.Columns.Add("PROGRESS", "Download Progress", 128); lvwUpdates.Columns.Add("SIZE", "Size", 128); lvwUpdates.Columns.Add("PATCH", "Patch For", 128); lsorter = new GenericListViewSorter(lvwUpdates, sortproc); lookupinfo.Clear(); //queued delegates to call after all other elements are added. //this is used for adding patches last. Queue <Action> DeferPatchItems = new Queue <Action>(); var Typelookup = new Dictionary <int, BCUpdate.UpdateInfo>(); try { foreach (BCUpdate.UpdateInfo looper in updateobj.LoadedUpdates) { var loopupdate = looper; //we copy to a local variable because otherwise how a foreach control variable //is closed over can be compiler specific. Dictionary <int, BCUpdate.UpdateInfo> typelookup = Typelookup; loopupdate.Tag = new DrawItemData("", null, null); Action loopbody = (() => { Color useForeColor = SystemColors.WindowText; String usepatchString = ""; if (loopupdate.DownloadFor > 0) { if (!typelookup.ContainsKey(loopupdate.DownloadFor)) { usepatchString = "<Unknown>"; } else { var appliedto = typelookup[loopupdate.DownloadFor]; usepatchString = appliedto.DlName; if (updateobj.getinstalledVersion(appliedto.dlID) == "") { useForeColor = SystemColors.GrayText; usepatchString = "<" + appliedto.DlName + " Not Installed>"; } } } string[] createdstrings = new string[] { loopupdate.DlName, updateobj.getinstalledVersion(loopupdate.dlID), loopupdate.UpdateVersion, "0", ByteSizeFormatter.FormatSize(loopupdate.FileSize), usepatchString }; ListViewItem newitem = new ListViewItem(createdstrings); ((DrawItemData)loopupdate.Tag).lvwitem = newitem; newitem.Tag = loopupdate; newitem.ForeColor = useForeColor; lookupinfo.Add(loopupdate, newitem); lvwUpdates.Items.Add(newitem); typelookup.Add(loopupdate.dlID, loopupdate); }); //if we need to defer it, add it to the queue. Otherwise, call it now. if (loopupdate.DownloadFor == 0) { loopbody(); } else { DeferPatchItems.Enqueue(loopbody); } } while (DeferPatchItems.Any()) { DeferPatchItems.Dequeue()(); } } catch (Exception except) { switch ( MessageBox.Show( "The Following Exception occured trying to retrieve update information:\n" + except.ToString(), "Unexpected Error", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error) ) { case DialogResult.Retry: goto tryagain; case DialogResult.Cancel: Close(); break; } } break; case EUpdateMode.Update_Immediate: //resize to Grp Debug.Print("Immediate Update..."); //grpBasicUpdate.Location = new Point(ClientRectangle.Left, ClientRectangle.Top); ClientSize = grpBasicUpdate.Size; fraavailupdates.Hide(); MinimizeBox = true; terminateonupdate = true; grpBasicUpdate.Visible = true; grpBasicUpdate.BringToFront(); this.Invalidate(); grpBasicUpdate.Invalidate(); grpBasicUpdate.Update(); this.Invalidate(); this.Update(); Debug.Print("grpBasicUpdate.Visible=" + grpBasicUpdate.Visible); immediateupdate.CancelDownload(); String downloadresult = immediateupdate.DownloadUpdate(progressroutine, completionroutine); if (updatingItems == null) { updatingItems = new List <BCUpdate.UpdateInfo>(); } updatingItems.Add(immediateupdate); break; } Cursor.Current = Cursors.Default; } catch (Exception exx) { panRefreshing.Visible = true; panRefreshing.BringToFront(); panRefreshing.Location = new Point(0, 0); panRefreshing.Size = new Size(ClientSize.Width, panLower.Top); lblrefreshing.Text = "An Exception occured retrieving update information."; txtException.Text = exx.ToString(); btnDownload.Visible = false; } }