From 341a13e523e4651f1b997b21b6aa6e96f469d94c Mon Sep 17 00:00:00 2001 From: Lynne Date: Thu, 3 Sep 2020 15:23:23 +1000 Subject: [PATCH] initial work on multithreaded updates --- Listing.cs | 2 ++ MainWindow.cs | 56 +++++++++++++++++++++++++++++++++++++++++---------- Settings.cs | 6 ++++++ 3 files changed, 53 insertions(+), 11 deletions(-) diff --git a/Listing.cs b/Listing.cs index 0e4dfd1..652f758 100644 --- a/Listing.cs +++ b/Listing.cs @@ -13,11 +13,13 @@ namespace Buypeeb { // start_date, end_date public int bids; public bool auto_extension; + public bool ready; public Listing(string url, string id, string name) { this.url = url; this.id = id; this.name = name; + this.ready = false; } public void Update() { diff --git a/MainWindow.cs b/MainWindow.cs index 954669b..71c5ce2 100755 --- a/MainWindow.cs +++ b/MainWindow.cs @@ -19,15 +19,18 @@ along with this program. If not, see . using System; using System.Collections.Generic; using System.Text.RegularExpressions; +// using System.Text.Json; +using System.Threading; +using System.Threading.Tasks; using Gtk; namespace Buypeeb { - enum ItemColumns { - Name, - PriceYen, - PriceAUD, - Ending, - Id + public struct ItemColumns { + public static int Name = 0; + public static int PriceYen = 1; + public static int PriceAUD = 2; + public static int Ending = 3; + public static int Id = 4; } class MainWindow : Window { @@ -35,17 +38,21 @@ namespace Buypeeb { private ListStore items; private Settings settings; + static SemaphoreSlim tasklimit = new SemaphoreSlim(4); + public MainWindow() : this(new Builder("main.glade")) { } private MainWindow(Builder builder) : base(builder.GetObject("wndMain").Handle) { this.settings = new Settings(); this.settings.Save(); this.Title = "Buypeeb"; + builder.Autoconnect(this); this.items = (ListStore)builder.GetObject("ListItems"); this.RenderList(); + foreach (object[] row in this.items) { - Console.WriteLine(row[(int)ItemColumns.Name]); + Console.WriteLine(row[ItemColumns.Name]); } DeleteEvent += Window_Shutdown; } @@ -56,6 +63,36 @@ namespace Buypeeb { // general behaviour + private void UpdateThread(string id) { + var item = this.settings.watchlist[id]; + Console.WriteLine($"Updating {id}..."); + item.ready = false; + Thread.Sleep(3000); + string html = "Heeenlo"; + item.name = html; + item.ready = true; + Console.WriteLine($"{id} updated."); + Gtk.Application.Invoke(delegate { + // this.RenderListItem(id);\ + this.RenderList(); + }); + } + + private void UpdateItem(string id) { + tasklimit.Wait(); + var t = Task.Factory.StartNew(() => { + this.UpdateThread(id); + }).ContinueWith(task => { tasklimit.Release(); }); + } + + private void UpdateItems() { + var t = Task.Factory.StartNew(() => { + foreach (var item in this.settings.watchlist) { + this.UpdateItem(item.Key); + } + }); + } + private (Boolean accepted, string response) EntryDialogue(string title = "Buypeeb", string message = "Hi there!") { Dialog ed = new Dialog(title, null, Gtk.DialogFlags.DestroyWithParent, "Cancel", ResponseType.Cancel, "OK", ResponseType.Ok); ed.DefaultResponse = ResponseType.Ok; @@ -75,10 +112,6 @@ namespace Buypeeb { return (accepted == ResponseType.Ok, response); } - private void UpdateItems() { - - } - private void RenderList() { this.items.Clear(); foreach (KeyValuePair entry in settings.watchlist) { @@ -111,6 +144,7 @@ namespace Buypeeb { private void ButtonUpdateAllClicked(object sender, EventArgs a) { Console.WriteLine("ButtonUpdateAllClicked"); + this.UpdateItems(); } private void ButtonClearEndedClicked(object sender, EventArgs a) { diff --git a/Settings.cs b/Settings.cs index 5ef5235..14240da 100644 --- a/Settings.cs +++ b/Settings.cs @@ -26,8 +26,14 @@ namespace Buypeeb { // ~/.config/Lynnear Software/buypeeb this.location = Path.Combine(Environment.GetEnvironmentVariable("HOME"), ".config", "Lynnear Software", "buypeeb"); } + this.watchlist = new Dictionary(); this.Watch("https://buypeeb.biz/whatever/k12345", "my thingy"); + this.Watch("https://buypeeb.biz/whatever/z09876", "your thingy"); + this.Watch("https://buypeeb.biz/whatever/h55555", "our thingy"); + for (int i = 0; i < 10; i++) { + this.Watch($"https://buypeeb.biz/whatever/x{i * 123}", $"filler {i}"); + } this.watchlist["k12345"].Update(); }