/// <summary> /// This method demonstrates how to close the device properly using the WinRT Serial API. /// /// When the SerialDevice is closing, it will cancel all IO operations that are still pending (not complete). /// The close will not wait for any IO completion callbacks to be called, so the close call may complete before any of /// the IO completion callbacks are called. /// The pending IO operations will still call their respective completion callbacks with either a task /// cancelled error or the operation completed. /// </summary> private async void CloseCurrentlyConnectedDevice() { if (Device != null) { // Notify callback that we're about to close the device OnDeviceClose?.Invoke(this, DeviceInformation); ActionQueue.Dispose(); Connection.Dispose(); // This closes the handle to the device Device.Dispose(); ActionQueue = null; Connection = null; Device = null; // Save the deviceInformation.Id in case deviceInformation is set to null when closing the // device var deviceName = DeviceInformation.Properties["System.ItemNameDisplay"] as string; await MainPage.Current.Dispatcher.RunAsync( CoreDispatcherPriority.Normal, () => { MainPage.Current.NotifyUser(deviceName + " is closed", NotifyType.StatusMessage); }); } }
protected override void OnNavigatedFrom(NavigationEventArgs e) { _videoEngine.Torch = false; _actionQueue.Dispose(); _settings.Save(); settingsPanelControl.ModeChanged -= _videoEngine.OnModeChanged; settingsPanelControl.RemoveNoiseChanged -= _videoEngine.OnRemoveNoiseChanged; settingsPanelControl.ApplyEffectOnlyChanged -= _videoEngine.OnApplyEffectOnlyChanged; settingsPanelControl.IsoChanged -= _videoEngine.OnIsoSettingsChangedAsync; settingsPanelControl.ExposureChanged -= _videoEngine.OnExposureSettingsChangedAsync; _videoEngine.ShowMessageRequest -= OnVideoEngineShowMessageRequestAsync; _videoEngine.Messenger.FrameCaptured -= OnFrameCapturedAsync; _videoEngine.Messenger.PostProcessComplete -= OnPostProcessCompleteAsync; Window.Current.VisibilityChanged -= OnVisibilityChangedAsync; captureElement.Source = null; _videoEngine.DisposeAsync(); base.OnNavigatedFrom(e); }
public void Run() { if ((null == m_q) || m_q.IsCompleted) { throw new InvalidOperationException("You must call Prepare() before calling Run()."); } foreach (Action action in m_q.GetConsumingEnumerable()) { // We do not exit early if cancellation has been requested, because // actions may need to be run during cancellation. m_cmdlet.IgnorePipelineStopped(action); } m_actionQueue.Dispose(); m_q.Dispose(); m_q = null; } // end Run()
public void Enqueue_PerformActionAndDispose() { var actionQueue = new ActionQueue<string>(s => Debug.WriteLine(s)); Task[] tasks = new Task[10]; for (int i = 0; i < tasks.Length; ++i) { int taskIndex = i; tasks[i] = Task.Run(() => { for (int j = 0; j < 10; ++j) { actionQueue.Enqueue(string.Format("task {0}; item #{1}", taskIndex, j)); } }); } Task.WaitAll(tasks); actionQueue.Dispose(); }
public async Task ActionQueue_DisposeSelf() { var aq = new ActionQueue(_ => { }); var disposeTask = aq.RunAsync(() => { aq.Dispose(); return(true); }); var task = await Task.WhenAny(disposeTask, Task.Delay(5000)); Assert.AreSame(disposeTask, task); var wontRunTask = aq.RunAsync(() => { return(true); }); task = await Task.WhenAny(wontRunTask, Task.Delay(500)); Assert.AreNotSame(wontRunTask, task); }