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
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() {

View file

@ -19,15 +19,18 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
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<string, Listing> 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) {

View file

@ -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<string, Listing>();
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();
}