diff --git a/TrackerBridge/DSP/GazeDecimator.cs b/TrackerBridge/DSP/GazeDecimator.cs index f897e9c..03a3b60 100644 --- a/TrackerBridge/DSP/GazeDecimator.cs +++ b/TrackerBridge/DSP/GazeDecimator.cs @@ -13,10 +13,10 @@ namespace TrackerBridge.DSP private GazeData output; private Single outputFrequency; + private Timer timer; private readonly Object outputLock = new Object(); private readonly Object valueCountLock = new Object(); private Int64 valueCount; - private Task timerTask; private Int64 ValueCount { get { @@ -51,7 +51,7 @@ namespace TrackerBridge.DSP public GazeDecimator(GazeDataProcessor dataProcessor) { outputFrequency = dataProcessor.OutputFrequency; - Task timerTask = Task.Run(() => TriggerOutput(outputFrequency)); + SetupTimer(); } public void Input(GazeData input, Object sender = null) { @@ -76,9 +76,11 @@ namespace TrackerBridge.DSP ValueCount = 0; } - private void TriggerOutput(Single frequency) + private void SetupTimer() { - Timer timer = new Timer(1000.0 / frequency); //TODO: Implement Dispose + timer?.Stop(); + timer?.Dispose(); + timer = new Timer(1000.0 / outputFrequency); timer.Elapsed += (Object sender, ElapsedEventArgs e) => { if (ValueCount > 0) @@ -94,9 +96,7 @@ namespace TrackerBridge.DSP public void Update(GazeDataProcessor dataProcessor) { outputFrequency = dataProcessor.OutputFrequency; - //timerTask.Dispose(); //TODO: Implement proper cancellation - //TODO: Fix memory leak - timerTask = Task.Run(() => TriggerOutput(outputFrequency)); + SetupTimer(); } } } diff --git a/TrackerBridge/FakeTracker.cs b/TrackerBridge/FakeTracker.cs index 629867b..126c96c 100644 --- a/TrackerBridge/FakeTracker.cs +++ b/TrackerBridge/FakeTracker.cs @@ -20,7 +20,7 @@ namespace TrackerBridge } private readonly GazeDataProcessor gazeDataProcessor; - private Task timerTask; + private System.Timers.Timer timer; private Single trackerFrequency; public Single OutputFrequency { @@ -33,9 +33,8 @@ namespace TrackerBridge return trackerFrequency; } set { - //timerTask.Dispose(); //TODO: Proper cancellation - //TODO: Fix memory leak - timerTask = Task.Run(() => SimulateGazeDataHz(value)); + trackerFrequency = value; + TimerSetup(); } } @@ -47,12 +46,15 @@ namespace TrackerBridge public void Connect() { - timerTask = Task.Run(() => SimulateGazeDataHz(trackerFrequency)); + TimerSetup(); ConnectionEvent?.Invoke(this); } - private void SimulateGazeDataHz(Single frequency) + + private void TimerSetup() { - System.Timers.Timer timer = new System.Timers.Timer(1000.0 / frequency); //TODO: Implement Dispose + timer?.Stop(); + timer?.Dispose(); + timer = new System.Timers.Timer(1000.0 / trackerFrequency); timer.Elapsed += (Object sender, ElapsedEventArgs e) => { Point mousePosition = Control.MousePosition;