From f17d39f37cac50861467e07a7bee40534d20100a Mon Sep 17 00:00:00 2001 From: Roy Ben Shabat Date: Wed, 4 Mar 2020 21:32:42 +0200 Subject: Improved "Notify Continuous Requests About Disconnection". Integrated FSE/PPC Remote Desktop. Implemented RemoteDesktopService / RemoteDesktopProvider. Implemented Mouse/Keyboard gestures. --- .../Visual_Studio/Tango.Core/Tango.Core.csproj | 4 +- .../Threading/IntervalMessageDispatcher.cs | 119 +++++++++++++++++++++ .../Tango.Core/Threading/SequencerThread.cs | 71 ------------ 3 files changed, 121 insertions(+), 73 deletions(-) create mode 100644 Software/Visual_Studio/Tango.Core/Threading/IntervalMessageDispatcher.cs delete mode 100644 Software/Visual_Studio/Tango.Core/Threading/SequencerThread.cs (limited to 'Software/Visual_Studio/Tango.Core') diff --git a/Software/Visual_Studio/Tango.Core/Tango.Core.csproj b/Software/Visual_Studio/Tango.Core/Tango.Core.csproj index 8014ac3f2..605ba53dc 100644 --- a/Software/Visual_Studio/Tango.Core/Tango.Core.csproj +++ b/Software/Visual_Studio/Tango.Core/Tango.Core.csproj @@ -98,7 +98,7 @@ - + @@ -206,7 +206,7 @@ - + diff --git a/Software/Visual_Studio/Tango.Core/Threading/IntervalMessageDispatcher.cs b/Software/Visual_Studio/Tango.Core/Threading/IntervalMessageDispatcher.cs new file mode 100644 index 000000000..ed61bea62 --- /dev/null +++ b/Software/Visual_Studio/Tango.Core/Threading/IntervalMessageDispatcher.cs @@ -0,0 +1,119 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +namespace Tango.Core.Threading +{ + /// + /// Represents a message queue dispatcher for delivering messages at a constant interval. + /// + /// + /// + public class IntervalMessageDispatcher : IDisposable + { + private Thread _queueThread; + private ProducerConsumerQueue _queue; + private Action _onNext; + + /// + /// Gets or sets the interval. + /// + public int Interval { get; set; } + + /// + /// Gets a value indicating whether this instance has started. + /// + public bool IsStarted { get; private set; } + + /// + /// Gets the number of queued messages. + /// + public int Count + { + get { return _queue.Count; } + } + + /// + /// Initializes a new instance of the class. + /// + /// The delivery callback. + public IntervalMessageDispatcher(Action onNext) + { + _onNext = onNext; + _queue = new ProducerConsumerQueue(); + } + + /// + /// Starts the dispatching of messages. + /// + public void Start() + { + if (!IsStarted) + { + IsStarted = true; + _queueThread = new Thread(QueueThreadMethod); + _queueThread.Name = "Sequencer Thread"; + _queueThread.IsBackground = true; + _queueThread.Start(); + } + } + + /// + /// Pushes the specified message. + /// + /// The message. + public void Push(TMessage message) + { + _queue.BlockEnqueue(message); + } + + private void QueueThreadMethod() + { + Stopwatch watch = new Stopwatch(); + watch.Start(); + + while (IsStarted) + { + watch.Restart(); + var item = _queue.BlockDequeue(); + + if (!IsStarted) break; + + try + { + _onNext?.Invoke(item); + } + catch { } + + Thread.Sleep(Math.Max(1, (int)(Interval - watch.ElapsedMilliseconds))); + } + } + + /// + /// Releases unmanaged and - optionally - managed resources. + /// + public void Dispose() + { + IsStarted = false; + } + + /// + /// Creates a new instance of and starts it immediately. + /// + /// + /// The on next. + /// The interval. + /// + public static IntervalMessageDispatcher StartNew(Action onNext, int interval) + { + IntervalMessageDispatcher dispatcher = new IntervalMessageDispatcher(onNext); + dispatcher.Interval = interval; + dispatcher.Start(); + return dispatcher; + } + } +} diff --git a/Software/Visual_Studio/Tango.Core/Threading/SequencerThread.cs b/Software/Visual_Studio/Tango.Core/Threading/SequencerThread.cs deleted file mode 100644 index 603790466..000000000 --- a/Software/Visual_Studio/Tango.Core/Threading/SequencerThread.cs +++ /dev/null @@ -1,71 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.Linq; -using System.Text; -using System.Threading; -using System.Threading.Tasks; - -namespace Tango.Core.Threading -{ - public class SequencerThread : IDisposable - { - private Thread _queueThread; - private ProducerConsumerQueue _queue; - private Action _onNext; - - public int FrameRate { get; set; } - - public bool IsStarted { get; private set; } - - public SequencerThread(Action onNext) - { - _onNext = onNext; - _queue = new ProducerConsumerQueue(); - } - - public void Start() - { - if (!IsStarted) - { - IsStarted = true; - _queueThread = new Thread(QueueThreadMethod); - _queueThread.Name = "Sequencer Thread"; - _queueThread.IsBackground = true; - _queueThread.Start(); - } - } - - public void Push(T item) - { - _queue.BlockEnqueue(item); - } - - private void QueueThreadMethod() - { - Stopwatch watch = new Stopwatch(); - watch.Start(); - - while (IsStarted) - { - watch.Restart(); - var item = _queue.BlockDequeue(); - - if (!IsStarted) break; - - try - { - _onNext?.Invoke(item); - } - catch { } - - Thread.Sleep(Math.Max(1, (int)(FrameRate - watch.ElapsedMilliseconds))); - } - } - - public void Dispose() - { - IsStarted = false; - } - } -} -- cgit v1.3.1