initial work on multithreaded updates

This commit is contained in:
Lynne Megido 2020-09-03 15:23:23 +10:00
parent 562b1330c9
commit 341a13e523
Signed by: lynnesbian
GPG key ID: F0A184B5213D9F90
3 changed files with 53 additions and 11 deletions

View file

@ -13,11 +13,13 @@ namespace Buypeeb {
// start_date, end_date // start_date, end_date
public int bids; public int bids;
public bool auto_extension; public bool auto_extension;
public bool ready;
public Listing(string url, string id, string name) { public Listing(string url, string id, string name) {
this.url = url; this.url = url;
this.id = id; this.id = id;
this.name = name; this.name = name;
this.ready = false;
} }
public void Update() { public void Update() {

View file

@ -19,15 +19,18 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
// using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Gtk; using Gtk;
namespace Buypeeb { namespace Buypeeb {
enum ItemColumns { public struct ItemColumns {
Name, public static int Name = 0;
PriceYen, public static int PriceYen = 1;
PriceAUD, public static int PriceAUD = 2;
Ending, public static int Ending = 3;
Id public static int Id = 4;
} }
class MainWindow : Window { class MainWindow : Window {
@ -35,17 +38,21 @@ namespace Buypeeb {
private ListStore items; private ListStore items;
private Settings settings; private Settings settings;
static SemaphoreSlim tasklimit = new SemaphoreSlim(4);
public MainWindow() : this(new Builder("main.glade")) { } public MainWindow() : this(new Builder("main.glade")) { }
private MainWindow(Builder builder) : base(builder.GetObject("wndMain").Handle) { private MainWindow(Builder builder) : base(builder.GetObject("wndMain").Handle) {
this.settings = new Settings(); this.settings = new Settings();
this.settings.Save(); this.settings.Save();
this.Title = "Buypeeb"; this.Title = "Buypeeb";
builder.Autoconnect(this); builder.Autoconnect(this);
this.items = (ListStore)builder.GetObject("ListItems"); this.items = (ListStore)builder.GetObject("ListItems");
this.RenderList(); this.RenderList();
foreach (object[] row in this.items) { foreach (object[] row in this.items) {
Console.WriteLine(row[(int)ItemColumns.Name]); Console.WriteLine(row[ItemColumns.Name]);
} }
DeleteEvent += Window_Shutdown; DeleteEvent += Window_Shutdown;
} }
@ -56,6 +63,36 @@ namespace Buypeeb {
// general behaviour // 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!") { 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); Dialog ed = new Dialog(title, null, Gtk.DialogFlags.DestroyWithParent, "Cancel", ResponseType.Cancel, "OK", ResponseType.Ok);
ed.DefaultResponse = ResponseType.Ok; ed.DefaultResponse = ResponseType.Ok;
@ -75,10 +112,6 @@ namespace Buypeeb {
return (accepted == ResponseType.Ok, response); return (accepted == ResponseType.Ok, response);
} }
private void UpdateItems() {
}
private void RenderList() { private void RenderList() {
this.items.Clear(); this.items.Clear();
foreach (KeyValuePair<string, Listing> entry in settings.watchlist) { foreach (KeyValuePair<string, Listing> entry in settings.watchlist) {
@ -111,6 +144,7 @@ namespace Buypeeb {
private void ButtonUpdateAllClicked(object sender, EventArgs a) { private void ButtonUpdateAllClicked(object sender, EventArgs a) {
Console.WriteLine("ButtonUpdateAllClicked"); Console.WriteLine("ButtonUpdateAllClicked");
this.UpdateItems();
} }
private void ButtonClearEndedClicked(object sender, EventArgs a) { private void ButtonClearEndedClicked(object sender, EventArgs a) {

View file

@ -26,8 +26,14 @@ namespace Buypeeb {
// ~/.config/Lynnear Software/buypeeb // ~/.config/Lynnear Software/buypeeb
this.location = Path.Combine(Environment.GetEnvironmentVariable("HOME"), ".config", "Lynnear Software", "buypeeb"); this.location = Path.Combine(Environment.GetEnvironmentVariable("HOME"), ".config", "Lynnear Software", "buypeeb");
} }
this.watchlist = new Dictionary<string, Listing>(); this.watchlist = new Dictionary<string, Listing>();
this.Watch("https://buypeeb.biz/whatever/k12345", "my thingy"); 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(); this.watchlist["k12345"].Update();
} }