public async System.Threading.Tasks.Task<MessageReceived> ReadTag(System.Threading.CancellationToken cancellationToken, TimeSpan timeout) { if (!IsSupported) { if (_dontThrowExpceptionWhenNotSupported) { return null; } throw new NotSupportedException("This device does not support NFC (or perhaps it's disabled)"); } _result = new TaskCompletionSource<MessageReceived>(); //needs a message type Task timeoutTask = null; if (timeout != default(TimeSpan)) { timeoutTask = Task.Delay(timeout); } using (cancellationToken.Register((s => ((TaskCompletionSource<MessageReceived>)s).TrySetCanceled()), _result)) { AttachEvents(); StartForegroundMonitoring(); if (timeoutTask != null) { await Task.WhenAny(timeoutTask, _result.Task); if (timeoutTask.IsCompleted) { throw new TimeoutException("NFC message not recieved in time"); } } if (_result.Task.IsCanceled) { StopForegroundDispatch(); DetachEvents(); return null; } MessageReceived result = await _result.Task; //We don't need to stop the foreground dispatch. Prior to the message been sent the application calls //OnPause which removes the foreground dispatch. By removing the events we prevent it from being added back. //StopForegroundDispatch(); DetachEvents(); return result; } }
public static async Task<MapPoint> DrawPointAsync(SceneView sceneView, System.Threading.CancellationToken cancellationToken) { var tcs = new TaskCompletionSource<MapPoint>(); var sketchlayer = CreateSketchLayer(sceneView); sketchlayer.Opacity = .5; Graphic pointGraphic = null; Action cleanupEvents = SetUpHandlers(sceneView, (p) => //On mouse move move graphic around { if (p != null) { if (pointGraphic == null) { pointGraphic = new Graphic(p, DefaultMarkerSymbol); sketchlayer.Graphics.Add(pointGraphic); } else pointGraphic.Geometry = p; } }, (p) => //View tapped - completes task and returns point { tcs.SetResult(p); } , null); Action cleanup = () => { cleanupEvents(); sceneView.GraphicsOverlays.Remove(sketchlayer); }; cancellationToken.Register(() => tcs.SetCanceled()); MapPoint result = null; try { result = await tcs.Task; } finally { cleanup(); } return result; }
/// <summary> /// Registers to Event System /// </summary> protected override void Awake() { base.Awake(); System.Register(this); }
/// <summary> /// This is an internal method called from ReadInternal method. /// </summary> /// <param name="buffer">The byte array, passed to Read method.</param> /// <param name="offset">The offset in the buffer array to begin writing.</param> /// <param name="count">The number of bytes to read.</param> /// <param name="timeout">Milliseconds before a time-out occurs.</param> /// <param name="ct">A cancellation_token will be signaled by user cancellation.</param> /// <returns> /// The result of Task contains the length of bytes read. This may be less than count. /// </returns> private Task<int> ReadPartial( Windows.Storage.Streams.IBuffer buffer, int offset, int count, int timeout, System.Threading.CancellationToken ct ) { // Buffer check. if ((int)buffer.Length < (offset + count)) { throw new ArgumentException("Capacity of buffer is not enough."); } var inputStream = this.cdcData.BulkInPipes[0].InputStream; var reader = new Windows.Storage.Streams.DataReader(inputStream); return Task.Run(async () => { // CancellationTokenSource to cancel tasks. var cancellationTokenSource = new System.Threading.CancellationTokenSource(); // LoadAsync task. var loadTask = reader.LoadAsync((uint)count).AsTask<uint>(cancellationTokenSource.Token); // A timeout task that completes after the specified delay. var timeoutTask = Task.Delay(timeout == Constants.InfiniteTimeout ? System.Threading.Timeout.Infinite : timeout, cancellationTokenSource.Token); // Cancel tasks by user's cancellation. bool canceledByUser = false; ct.Register(()=> { canceledByUser = true; cancellationTokenSource.Cancel(); }); // Wait tasks. Task[] tasks = { loadTask, timeoutTask }; var signaledTask = await Task.WhenAny(tasks); // Check the task status. bool loadCompleted = signaledTask.Equals(loadTask) && loadTask.IsCompleted && !loadTask.IsCanceled; bool isTimeout = signaledTask.Equals(timeoutTask) && timeoutTask.IsCompleted && !timeoutTask.IsCanceled; // Cancel all incomplete tasks. cancellationTokenSource.Cancel(); int loadedCount = 0; if (loadCompleted) { loadedCount = (int)loadTask.Result; } else if (isTimeout) { // Timeout. throw new System.TimeoutException("ReadPartial was timeout."); } else if (canceledByUser) { throw new OperationCanceledException("ReadPartial was canceled."); } if (loadedCount > 0) { var readBuffer = reader.ReadBuffer((uint)loadedCount); System.Runtime.InteropServices.WindowsRuntime.WindowsRuntimeBufferExtensions.CopyTo(readBuffer, 0, buffer, (uint)offset, (uint)loadedCount); } return loadedCount; }); }
public override void Start(System.Threading.CancellationToken token){ base.Start(token); token.Register(Finish); }
public static async Task<Polyline> DrawPolylineAsync(SceneView sceneView, System.Threading.CancellationToken cancellationToken) { var tcs = new TaskCompletionSource<Polyline>(); PolylineBuilder polylineBuilder = new PolylineBuilder(sceneView.SpatialReference); var sketchlayer = CreateSketchLayer(sceneView); Graphic lineGraphic = new Graphic() { Symbol = DefaultLineSymbol }; Graphic lineMoveGraphic = new Graphic() { Symbol = DefaultLineMoveSymbol }; sketchlayer.Graphics.AddRange(new Graphic[] { lineGraphic, lineMoveGraphic }); Action cleanupEvents = SetUpHandlers(sceneView, (p) => //On mouse move, move completion line around { if (p != null && polylineBuilder.Parts.Count > 0 && polylineBuilder.Parts[0].Count > 0) { lineMoveGraphic.Geometry = new Polyline(new MapPoint[] { polylineBuilder.Parts[0].Last().EndPoint, p }); } }, (p) => //On tap add a vertex { if (p != null) { polylineBuilder.AddPoint(p); if (polylineBuilder.Parts.Count > 0 && polylineBuilder.Parts[0].Count >= 1) lineGraphic.Geometry = polylineBuilder.ToGeometry(); } }, (p) => //View tapped - completes task and returns point { tcs.SetResult(polylineBuilder.ToGeometry()); }); Action cleanup = () => { cleanupEvents(); sceneView.GraphicsOverlays.Remove(sketchlayer); }; cancellationToken.Register(() => tcs.SetCanceled()); Polyline result = null; try { result = await tcs.Task; } finally { cleanup(); } return result; }