From c38f1c80f1fbdfdb758c5a0b93d045a9a5b526ad Mon Sep 17 00:00:00 2001 From: Roy Ben-Shabat Date: Tue, 3 Mar 2020 18:56:58 +0200 Subject: Machine Studio v4.1.2 PPC v1.1.5 Added BYPASS_ROCKERS to SQLExaminer config. Started integrating FSE Remote/Console To PPC. Added support for generic continuous request. --- .../Visual_Studio/Tango.Core/Tango.Core.csproj | 3 +- .../Tango.Core/Threading/SequencerThread.cs | 71 ++++++++++++++++++++++ 2 files changed, 73 insertions(+), 1 deletion(-) create 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 7c794c276..8014ac3f2 100644 --- a/Software/Visual_Studio/Tango.Core/Tango.Core.csproj +++ b/Software/Visual_Studio/Tango.Core/Tango.Core.csproj @@ -98,6 +98,7 @@ + @@ -205,7 +206,7 @@ - + diff --git a/Software/Visual_Studio/Tango.Core/Threading/SequencerThread.cs b/Software/Visual_Studio/Tango.Core/Threading/SequencerThread.cs new file mode 100644 index 000000000..603790466 --- /dev/null +++ b/Software/Visual_Studio/Tango.Core/Threading/SequencerThread.cs @@ -0,0 +1,71 @@ +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