Ejemplo n.º 1
0
        /// <summary>
        /// Initialises the device for asynchronous reads.
        /// </summary>
        public void Initialize()
        {
            for (ulong count = 0; count < readBufferCount; count++)
            {
                this.readBuffers.Add(new byte[this.ReadLength]);
            }

            lastBuffer = new byte[this.ReadLength];

            ContinueProcessing      = true;
            CancellationTokenSource = new CancellationTokenSource();

            // StartNew wraps the task.  Call Unwrap() to get the real task.
            SerialProcessingTask = Task.Factory.StartNew(async() => await ReadSerial(CancellationTokenSource.Token).ConfigureAwait(false),
                                                         CancellationTokenSource.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default).Unwrap();

            // Log all the ways that this can stop
            SerialProcessingTask.ContinueWith(t => {
                if (t.IsCanceled)
                {
                    logger?.LogDebug("Controller ReadSerial() cancelled");
                }
                else if (t.IsFaulted)
                {
                    logger?.LogError($"Controller ReadSerial() Exception: {t.Exception.InnerException?.Message}");
                }
                else
                {
                    logger?.LogDebug("Controller ReadSerial() complete");
                }
            });
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Stops the reader.
 /// </summary>
 public void Stop()
 {
     ContinueProcessing = false;
     CancellationTokenSource?.Cancel();
     SerialProcessingTask?.Wait();
 }