using bzit.bomg.Models; using RyzStudio.Windows.Forms; using System; using System.Collections.Generic; using System.ComponentModel; using System.Threading.Tasks; using System.Windows.Forms; using Form = System.Windows.Forms.Form; using Resources = bzit.bomg.Properties.Resources; namespace bzit.bomg { public partial class UpdateIconsForm : Form { protected bool isBusy = false; protected bool requestCancel = false; protected BookmarkItemModel itemModel = null; protected MainForm parentForm = null; public UpdateIconsForm(MainForm mainForm) : base() { InitializeComponent(); parentForm = mainForm; this.StartPosition = FormStartPosition.WindowsDefaultLocation; } protected override void OnClosing(CancelEventArgs e) { base.OnClosing(e); if (this.IsBusy) { e.Cancel = true; } } protected bool IsBusy { get => isBusy; set { isBusy = value; ThreadControl.SetImage(pictureBox1, (value) ? Resources.aniZomq2x32 : null); ThreadControl.SetEnable(button2, !value); } } private async void button1_Click(object sender, EventArgs e) { if (this.IsBusy) { requestCancel = true; ThreadControl.SetText(button1, "&Cancelling..."); return; } this.IsBusy = true; await Task.Run(() => { ThreadControl.SetText(button1, "&Updating..."); if (requestCancel) { this.IsBusy = false; ThreadControl.SetText(button1, "&Update"); requestCancel = false; return; } List nodeList = parentForm.treeView1.GetBookmarkNodeList(); ThreadControl.SetValue(progressBar1, 0, nodeList.Count); for (int i = 0; i < nodeList.Count; i++) { if (requestCancel) { this.IsBusy = false; ThreadControl.SetText(button1, "&Update"); requestCancel = false; return; } ThreadControl.SetValue(progressBar1, (i + 1)); BookmarkItemViewModel viewModel = (BookmarkItemViewModel)nodeList[i].Tag; BookmarkItemModel model = viewModel.ToModel(); bool rv = model.UpdateFavicon(); if (rv) { if (parentForm.treeView1.InvokeRequired) { parentForm.treeView1.Invoke(new MethodInvoker(() => { parentForm.treeView1.UpdateItem(nodeList[i], model.ToViewModel()); })); } else { parentForm.treeView1.UpdateItem(nodeList[i], model.ToViewModel()); } } } this.IsBusy = false; ThreadControl.SetText(button1, "&Update"); requestCancel = false; }); } private void button2_Click(object sender, EventArgs e) { if (this.IsBusy) { return; } this.Close(); } } }