This repository has been archived on 2022-09-30. You can view files and clone it, but cannot push or open issues or pull requests.
bookmark-manager/UpdateIconsForm.cs
2020-08-29 23:46:36 +01:00

131 lines
3.6 KiB
C#

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<TreeNode> nodeList = parentForm.TreeView.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.TreeView.InvokeRequired)
{
parentForm.TreeView.Invoke(new MethodInvoker(() =>
{
parentForm.TreeView.UpdateItem(nodeList[i], model.ToViewModel());
}));
}
else
{
parentForm.TreeView.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();
}
}
}