コード例 #1
0
        public static async Task StartAsync <TProvider>(SmartConfigArguments args,
                                                        CancellationToken cancelToken,
                                                        Action <object, DeviceDiscoveredEventArgs> onDeviceDiscovered = null,
                                                        Action <object, SmartConfigTimerEventArgs> onElapsed          = null,
                                                        int timeoutSeconds = 100)
            where TProvider : class, ISmartConfigProvider, new()
        {
            if (args == null)
            {
                throw new ArgumentNullException(nameof(args));
            }

            var provider = new TProvider();
            var ctx      = provider.CreateContext();

            if (onDeviceDiscovered != null)
            {
                ctx.DeviceDiscoveredEvent += new EventHandler <DeviceDiscoveredEventArgs>(onDeviceDiscovered);
            }
            using (var job = new SmartConfigJob(TimeSpan.FromSeconds(timeoutSeconds)))
            {
                if (onElapsed != null)
                {
                    job.Elapsed += new SmartConfigTimerEventHandler(onElapsed);
                }
                await job.ExecuteAsync(ctx, args, cancelToken);
            }
        }
コード例 #2
0
 public static async Task StartAsync <TProvider>(SmartConfigArguments args,
                                                 Action <object, DeviceDiscoveredEventArgs> onDeviceDiscovered = null,
                                                 Action <object, SmartConfigTimerEventArgs> onElapsed          = null,
                                                 int timeoutSeconds = 100)
     where TProvider : class, ISmartConfigProvider, new()
 {
     await StartAsync <TProvider>(args, CancellationToken.None, onDeviceDiscovered, onElapsed, timeoutSeconds);
 }
コード例 #3
0
        public async Task ExecuteAsync(SmartConfigContext context, SmartConfigArguments args, CancellationToken externalCancelToken)
        {
            if (_isStarted)
            {
                throw new InvalidOperationException("Already started");
            }

            this.ExecutedTime = TimeSpan.Zero;
            _isStarted        = true;
            _timerCts         = new CancellationTokenSource();
            var linkedCts = CancellationTokenSource.CreateLinkedTokenSource(externalCancelToken, _timerCts.Token);

            try
            {
                _timer.Start();
                this.Elapsed(this, new SmartConfigTimerEventArgs(this.Timeout, this.ExecutedTime));

                var broadcastingTask = _broadcaster.BroadcastAsync(context, args, linkedCts.Token).CancelOnFaulted(linkedCts);
                var receivingTask    = _receiver.ListenAsync(context, linkedCts.Token).CancelOnFaulted(linkedCts);
                await Task.WhenAll(broadcastingTask, receivingTask);
            }
            catch (OperationCanceledException ocex)
            {
                if (externalCancelToken.IsCancellationRequested)
                {
                    throw ocex;
                }
            }
            catch
            {
                linkedCts.Cancel();
                throw;
            }
            finally
            {
                if (_timer.Enabled)
                {
                    _timer.Stop();
                }

                linkedCts.Dispose();

                _timerCts.Dispose();
                _timerCts = null;

                ExecutedTime = TimeSpan.Zero;
                _isStarted   = false;
            }
        }
コード例 #4
0
 public static async Task ExecuteAsync(
     this ISmartConfigJob self, SmartConfigArguments args, SmartConfigContext context)
 {
     await self.ExecuteAsync(context, args, CancellationToken.None);
 }