Changed: tasks instead of background workers
This commit is contained in:
		
							parent
							
								
									9e6992acd2
								
							
						
					
					
						commit
						9043901c73
					
				| @ -1,8 +1,8 @@ | |||||||
| using bzit.bomg.Models; | using bzit.bomg.Models; | ||||||
| using RyzStudio.Windows.Forms; | using RyzStudio.Windows.Forms; | ||||||
| using RyzStudio.Windows.ThemedForms; |  | ||||||
| using System; | using System; | ||||||
| using System.ComponentModel; | using System.ComponentModel; | ||||||
|  | using System.Threading.Tasks; | ||||||
| using System.Windows.Forms; | using System.Windows.Forms; | ||||||
| using Form = System.Windows.Forms.Form; | using Form = System.Windows.Forms.Form; | ||||||
| using Resources = bzit.bomg.Properties.Resources; | using Resources = bzit.bomg.Properties.Resources; | ||||||
| @ -11,8 +11,7 @@ namespace bzit.bomg | |||||||
| { | { | ||||||
|     public partial class BookmarkEditForm : Form |     public partial class BookmarkEditForm : Form | ||||||
|     { |     { | ||||||
|         protected BackgroundWorker threadWorker = null; |         protected bool isBusy = false; | ||||||
| 
 |  | ||||||
|         protected BookmarkItemModel itemModel = null; |         protected BookmarkItemModel itemModel = null; | ||||||
|         protected BookmarkTreeView treeView = null; |         protected BookmarkTreeView treeView = null; | ||||||
| 
 | 
 | ||||||
| @ -23,38 +22,6 @@ namespace bzit.bomg | |||||||
|             treeView = treeview; |             treeView = treeview; | ||||||
| 
 | 
 | ||||||
|             this.StartPosition = FormStartPosition.WindowsDefaultLocation; |             this.StartPosition = FormStartPosition.WindowsDefaultLocation; | ||||||
| 
 |  | ||||||
|             if (threadWorker == null) |  | ||||||
|             { |  | ||||||
|                 threadWorker = new BackgroundWorker(); |  | ||||||
|                 threadWorker.WorkerReportsProgress = threadWorker.WorkerSupportsCancellation = true; |  | ||||||
|                 threadWorker.DoWork += threadWorker1_DoWork; |  | ||||||
|                 threadWorker.RunWorkerCompleted += threadWorker1_RunWorkerCompleted; |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
| 
 |  | ||||||
|         protected void threadWorker1_DoWork(object sender, DoWorkEventArgs e) |  | ||||||
|         { |  | ||||||
|             if (itemModel == null) |  | ||||||
|             { |  | ||||||
|                 itemModel = new BookmarkItemModel(); |  | ||||||
|             } |  | ||||||
| 
 |  | ||||||
|             itemModel.SiteAddress = textBox2.Text; |  | ||||||
| 
 |  | ||||||
|             bool rv = itemModel.Update(); |  | ||||||
|             if (rv) |  | ||||||
|             { |  | ||||||
|                 ThreadControl.SetText(textBox1, itemModel.SiteName); |  | ||||||
|                 ThreadControl.SetText(textBox2, itemModel.SiteAddress); |  | ||||||
|                 ThreadControl.SetText(memoBox1, itemModel.SiteDescription); |  | ||||||
|                 ThreadControl.SetImage(pictureBox2, itemModel.RetrieveFavicon()); |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
| 
 |  | ||||||
|         protected void threadWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) |  | ||||||
|         { |  | ||||||
|             this.IsBusy = false; |  | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         protected override void OnShown(EventArgs e) |         protected override void OnShown(EventArgs e) | ||||||
| @ -88,9 +55,10 @@ namespace bzit.bomg | |||||||
| 
 | 
 | ||||||
|         protected bool IsBusy |         protected bool IsBusy | ||||||
|         { |         { | ||||||
|             get => threadWorker.IsBusy; |             get => isBusy; | ||||||
|             set |             set | ||||||
|             { |             { | ||||||
|  |                 isBusy = value; | ||||||
|                 textBox1.Enabled = textBox2.Enabled = memoBox1.Enabled = !value; |                 textBox1.Enabled = textBox2.Enabled = memoBox1.Enabled = !value; | ||||||
| 
 | 
 | ||||||
|                 pictureBox1.Image = (value) ? Resources.aniZomq2x32 : null; |                 pictureBox1.Image = (value) ? Resources.aniZomq2x32 : null; | ||||||
| @ -110,7 +78,7 @@ namespace bzit.bomg | |||||||
|             pictureBox2.Image = treeView.ImageList.Images[treeView.SelectedNode.ImageIndex]; |             pictureBox2.Image = treeView.ImageList.Images[treeView.SelectedNode.ImageIndex]; | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         private void button1_Click(object sender, EventArgs e) |         private async void button1_Click(object sender, EventArgs e) | ||||||
|         { |         { | ||||||
|             if (this.IsBusy) |             if (this.IsBusy) | ||||||
|             { |             { | ||||||
| @ -124,30 +92,33 @@ namespace bzit.bomg | |||||||
| 
 | 
 | ||||||
|             this.IsBusy = true; |             this.IsBusy = true; | ||||||
| 
 | 
 | ||||||
|             threadWorker.RunWorkerAsync(); |             //threadWorker.RunWorkerAsync(); | ||||||
|  |             await Task.Run(() => | ||||||
|  |             { | ||||||
|  |                 if (itemModel == null) itemModel = new BookmarkItemModel(); | ||||||
|  | 
 | ||||||
|  |                 itemModel.SiteAddress = textBox2.Text; | ||||||
|  | 
 | ||||||
|  |                 bool rv = itemModel.Update(); | ||||||
|  |                 if (rv) | ||||||
|  |                 { | ||||||
|  |                     ThreadControl.SetText(textBox1, itemModel.SiteName); | ||||||
|  |                     ThreadControl.SetText(textBox2, itemModel.SiteAddress); | ||||||
|  |                     ThreadControl.SetText(memoBox1, itemModel.SiteDescription); | ||||||
|  |                     ThreadControl.SetImage(pictureBox2, itemModel.RetrieveFavicon()); | ||||||
|  |                 } | ||||||
|  |             }); | ||||||
|  | 
 | ||||||
|  |             this.IsBusy = false; | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         private void button2_Click(object sender, EventArgs e) |         private void button2_Click(object sender, EventArgs e) | ||||||
|         { |         { | ||||||
|             if (this.IsBusy) |             if (this.IsBusy) return; | ||||||
|             { |             if (string.IsNullOrWhiteSpace(textBox1.Text)) return; | ||||||
|                 return; |             if (string.IsNullOrWhiteSpace(textBox2.Text)) return; | ||||||
|             } |  | ||||||
| 
 | 
 | ||||||
|             if (string.IsNullOrWhiteSpace(textBox1.Text)) |             if (itemModel == null) itemModel = new BookmarkItemModel(); | ||||||
|             { |  | ||||||
|                 return; |  | ||||||
|             } |  | ||||||
| 
 |  | ||||||
|             if (string.IsNullOrWhiteSpace(textBox2.Text)) |  | ||||||
|             { |  | ||||||
|                 return; |  | ||||||
|             } |  | ||||||
| 
 |  | ||||||
|             if (itemModel == null) |  | ||||||
|             { |  | ||||||
|                 itemModel = new BookmarkItemModel(); |  | ||||||
|             } |  | ||||||
| 
 | 
 | ||||||
|             itemModel.SiteName = textBox1.Text?.Trim(); |             itemModel.SiteName = textBox1.Text?.Trim(); | ||||||
|             itemModel.SiteAddress = textBox2.Text?.Trim(); |             itemModel.SiteAddress = textBox2.Text?.Trim(); | ||||||
| @ -157,5 +128,6 @@ namespace bzit.bomg | |||||||
| 
 | 
 | ||||||
|             this.Close(); |             this.Close(); | ||||||
|         } |         } | ||||||
|  | 
 | ||||||
|     } |     } | ||||||
| } | } | ||||||
| @ -77,6 +77,13 @@ namespace bzit.bomg | |||||||
| 
 | 
 | ||||||
|             // favicon |             // favicon | ||||||
|             this.FaviconAddress = parseSiteIcon(document); |             this.FaviconAddress = parseSiteIcon(document); | ||||||
|  | 
 | ||||||
|  |             // default infurred icon | ||||||
|  |             if (string.IsNullOrWhiteSpace(this.FaviconAddress)) | ||||||
|  |             { | ||||||
|  |                 this.FaviconAddress = "/favicon.ico"; | ||||||
|  |             } | ||||||
|  | 
 | ||||||
|             if (!string.IsNullOrWhiteSpace(this.FaviconAddress)) |             if (!string.IsNullOrWhiteSpace(this.FaviconAddress)) | ||||||
|             { |             { | ||||||
|                 Uri iconAddressURI; |                 Uri iconAddressURI; | ||||||
| @ -103,6 +110,13 @@ namespace bzit.bomg | |||||||
| 
 | 
 | ||||||
|             // favicon |             // favicon | ||||||
|             this.FaviconAddress = parseSiteIcon(document); |             this.FaviconAddress = parseSiteIcon(document); | ||||||
|  | 
 | ||||||
|  |             // default infurred icon | ||||||
|  |             if (string.IsNullOrWhiteSpace(this.FaviconAddress)) | ||||||
|  |             { | ||||||
|  |                 this.FaviconAddress = "/favicon.ico"; | ||||||
|  |             } | ||||||
|  | 
 | ||||||
|             if (!string.IsNullOrWhiteSpace(this.FaviconAddress)) |             if (!string.IsNullOrWhiteSpace(this.FaviconAddress)) | ||||||
|             { |             { | ||||||
|                 Uri iconAddressURI; |                 Uri iconAddressURI; | ||||||
| @ -305,7 +319,7 @@ namespace bzit.bomg | |||||||
| 				rs = parseTagValue_Attr(doc, "//meta[@itemprop='name']", "content", string.Empty); | 				rs = parseTagValue_Attr(doc, "//meta[@itemprop='name']", "content", string.Empty); | ||||||
| 			} | 			} | ||||||
| 
 | 
 | ||||||
| 			return rs; | 			return rs?.Trim(); | ||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
| 		protected string parseSiteDescription(HtmlDocument doc) | 		protected string parseSiteDescription(HtmlDocument doc) | ||||||
| @ -394,5 +408,6 @@ namespace bzit.bomg | |||||||
| 
 | 
 | ||||||
|             return rs; |             return rs; | ||||||
| 		} | 		} | ||||||
|  | 
 | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
		Reference in New Issue
	
	Block a user
	 Ray
						Ray