Extract Periodic class from connection's Worker
This commit is contained in:
parent
76aca12fec
commit
94df48db7b
@ -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<void> thread;
|
||||
|
||||
public Worker(SessionManager session_manager) {
|
||||
base(TASK_PERIOD_MS);
|
||||
this.session_manager = session_manager;
|
||||
exit = false;
|
||||
thread = new Thread<void>("connection_worker", body);
|
||||
}
|
||||
|
||||
~Worker() {
|
||||
exit = true;
|
||||
thread.join();
|
||||
}
|
||||
|
||||
private void body() {
|
||||
while (!exit) {
|
||||
protected override void task() {
|
||||
session_manager.task();
|
||||
Thread.usleep(1000 * TASK_PERIOD_MS);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -17,6 +17,7 @@ lib = library(
|
||||
'connection.vala',
|
||||
'der.vala',
|
||||
'main_window.vala',
|
||||
'periodic.vala',
|
||||
'request.vala',
|
||||
'response.vala',
|
||||
'session_manager.vala',
|
||||
|
27
client/src/periodic.vala
Normal file
27
client/src/periodic.vala
Normal file
@ -0,0 +1,27 @@
|
||||
namespace StudySystemClient {
|
||||
public abstract class Periodic {
|
||||
private uint period_ms;
|
||||
private bool exit;
|
||||
private Thread<void> thread;
|
||||
|
||||
protected Periodic(uint period_ms) {
|
||||
this.period_ms = period_ms;
|
||||
exit = false;
|
||||
thread = new Thread<void>("Periodic task", body);
|
||||
}
|
||||
|
||||
~Periodic() {
|
||||
exit = true;
|
||||
thread.join();
|
||||
}
|
||||
|
||||
protected abstract void task();
|
||||
|
||||
private void body() {
|
||||
while (!exit) {
|
||||
task();
|
||||
Thread.usleep(1000 * period_ms);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user