diff --git a/client/src/connection.vala b/client/src/connection.vala index 7220f5f..1102dea 100644 --- a/client/src/connection.vala +++ b/client/src/connection.vala @@ -84,29 +84,18 @@ namespace StudySystemClient { } } - private class Worker { - private uint TASK_PERIOD_MS = 10; + private class Worker : Periodic { + private const uint TASK_PERIOD_MS = 10; private SessionManager session_manager; - private bool exit; - private Thread thread; public Worker(SessionManager session_manager) { + base(TASK_PERIOD_MS); this.session_manager = session_manager; - exit = false; - thread = new Thread("connection_worker", body); } - ~Worker() { - exit = true; - thread.join(); - } - - private void body() { - while (!exit) { - session_manager.task(); - Thread.usleep(1000 * TASK_PERIOD_MS); - } + protected override void task() { + session_manager.task(); } } } diff --git a/client/src/meson.build b/client/src/meson.build index 274ce49..7b7bad1 100644 --- a/client/src/meson.build +++ b/client/src/meson.build @@ -17,6 +17,7 @@ lib = library( 'connection.vala', 'der.vala', 'main_window.vala', + 'periodic.vala', 'request.vala', 'response.vala', 'session_manager.vala', diff --git a/client/src/periodic.vala b/client/src/periodic.vala new file mode 100644 index 0000000..bab1281 --- /dev/null +++ b/client/src/periodic.vala @@ -0,0 +1,27 @@ +namespace StudySystemClient { + public abstract class Periodic { + private uint period_ms; + private bool exit; + private Thread thread; + + protected Periodic(uint period_ms) { + this.period_ms = period_ms; + exit = false; + thread = new Thread("Periodic task", body); + } + + ~Periodic() { + exit = true; + thread.join(); + } + + protected abstract void task(); + + private void body() { + while (!exit) { + task(); + Thread.usleep(1000 * period_ms); + } + } + } +}