diff options
| author | Roy Ben-Shabat <Roy@Twine-s.com> | 2020-03-03 18:56:58 +0200 |
|---|---|---|
| committer | Roy Ben-Shabat <Roy@Twine-s.com> | 2020-03-03 18:56:58 +0200 |
| commit | c38f1c80f1fbdfdb758c5a0b93d045a9a5b526ad (patch) | |
| tree | 20cc57b06f4260b6f86fdaca04129e1a8ace53cd /Software/Visual_Studio/Tango.Core/Threading | |
| parent | 1b0bdf6f8148e9cc4e7e07e41e9e2d75039c1349 (diff) | |
| download | Tango-c38f1c80f1fbdfdb758c5a0b93d045a9a5b526ad.tar.gz Tango-c38f1c80f1fbdfdb758c5a0b93d045a9a5b526ad.zip | |
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.
Diffstat (limited to 'Software/Visual_Studio/Tango.Core/Threading')
| -rw-r--r-- | Software/Visual_Studio/Tango.Core/Threading/SequencerThread.cs | 71 |
1 files changed, 71 insertions, 0 deletions
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<T> : IDisposable + { + private Thread _queueThread; + private ProducerConsumerQueue<T> _queue; + private Action<T> _onNext; + + public int FrameRate { get; set; } + + public bool IsStarted { get; private set; } + + public SequencerThread(Action<T> onNext) + { + _onNext = onNext; + _queue = new ProducerConsumerQueue<T>(); + } + + 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; + } + } +} |
