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

131 lines
3.6 KiB
C#
Raw Permalink Normal View History

2020-08-28 22:05:59 +00:00
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;
}
2020-08-29 22:46:36 +00:00
List<TreeNode> nodeList = parentForm.TreeView.GetBookmarkNodeList();
2020-08-28 22:05:59 +00:00
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)
{
2020-08-29 22:46:36 +00:00
if (parentForm.TreeView.InvokeRequired)
2020-08-28 22:05:59 +00:00
{
2020-08-29 22:46:36 +00:00
parentForm.TreeView.Invoke(new MethodInvoker(() =>
2020-08-28 22:05:59 +00:00
{
2020-08-29 22:46:36 +00:00
parentForm.TreeView.UpdateItem(nodeList[i], model.ToViewModel());
2020-08-28 22:05:59 +00:00
}));
}
else
{
2020-08-29 22:46:36 +00:00
parentForm.TreeView.UpdateItem(nodeList[i], model.ToViewModel());
2020-08-28 22:05:59 +00:00
}
}
}
this.IsBusy = false;
ThreadControl.SetText(button1, "&Update");
requestCancel = false;
});
}
private void button2_Click(object sender, EventArgs e)
{
if (this.IsBusy)
{
return;
}
this.Close();
}
}
}