public Task<bool> RecordAsync(StorageFile file, CancellationToken cancToken) { return RunFunctionIfOpenAsyncTB(async delegate { if (file == null) return false; bool isOk = await StartRecordingAsync(file).ConfigureAwait(false); // Lock the thread asynchronously until explicitly closed from the caller. // If an error prevented starting recording, stay open to display error messages. // The lock will last until the cancellation token is cancelled or the semaphore is released or disposed. _recordLockingSemaphore = new SemaphoreSlimSafeRelease(0, 1); // this semaphore is always closed at the beginning try { await _recordLockingSemaphore.WaitAsync(cancToken).ConfigureAwait(false); Debug.WriteLine("I am past _triggerSemaphore"); } catch (OperationCanceledException) { isOk = false; Debug.WriteLine("I am past _triggerSemaphore after OperationCanceledException"); } catch (Exception ex) { Debug.WriteLine("I am past _triggerSemaphore after exception: " + ex.ToString()); if (SemaphoreSlimSafeRelease.IsAlive(_recordLockingSemaphore)) { isOk = false; Logger.Add_TPL(ex.ToString(), Logger.ForegroundLogFilename); } } return isOk; }); }
public static async Task <string> ReadAsync(string fileName) { string result = string.Empty; try { await _semaphore.WaitAsync().ConfigureAwait(false); result = await Read2Async(fileName).ConfigureAwait(false); } catch (FileNotFoundException exc0) { //put up with it: it may be the first time we use this file Debug.WriteLine("no worries but: " + exc0.ToString()); } catch (Exception exc) { if (SemaphoreSlimSafeRelease.IsAlive(_semaphore)) { Debug.WriteLine("ERROR in Logger.ReadAsync(): " + exc.ToString()); } } finally { SemaphoreSlimSafeRelease.TryRelease(_semaphore); } return(result); }
public static bool TryRelease(SemaphoreSlimSafeRelease semaphore, int releaseCount) { try { if (IsAlive(semaphore)) { semaphore.Release(releaseCount); return true; } } catch { } return false; }
public static bool TryRelease(SemaphoreSlimSafeRelease semaphore, int releaseCount) { try { if (IsAlive(semaphore)) { semaphore.Release(releaseCount); return(true); } } catch { } return(false); }
/// <summary> /// Disposes of a semaphore. /// You can use this method to interrupt all operations, which are queuing at the semaphore, if you initialised it for max 1 thread. /// Wait at the semaphore before calling this method, to be sure you don't disrupt any running operations at an unexpected moment. /// Every method using this semaphore should make allowance for it being disposed. /// </summary> /// <example> /// Use it as follows: /// <code> /// try /// { /// semaphore.Wait(); /// do some work /// } /// catch (Exception ex) /// { /// if (SemaphoreSlimSafeRelease.IsAlive(semaphore)) /// // you have a real error, otherwise it's simply the semaphore being disposed and reset to null /// } /// finally /// { /// SemaphoreSlimSafeRelease.TryRelease(semaphore); /// // do not just call semaphore.Release() because the semaphore may be disposed or null at this stage /// } /// </code> /// </example> /// <param name="semaphore"></param> /// <returns></returns> public static bool TryDispose(SemaphoreSlimSafeRelease semaphore) { try { if (IsAlive(semaphore)) { semaphore.Dispose(); return(true); } } catch { } return(false); }
public static bool TryRelease(SemaphoreSlimSafeRelease semaphore) { try { // the following commented out stuff wouldbe nice to avoid the exception but it is not atomic, so we leave it. if (IsAlive(semaphore) /*&& semaphore.CurrentCount < semaphore._maxCount*/) { semaphore.Release(); return true; } } catch { } return false; }
public static bool TryRelease(SemaphoreSlimSafeRelease semaphore) { try { // the following commented out stuff wouldbe nice to avoid the exception but it is not atomic, so we leave it. if (IsAlive(semaphore) /*&& semaphore.CurrentCount < semaphore._maxCount*/) { semaphore.Release(); return(true); } } catch { } return(false); }
public static void ClearAll() { Task.Run(async delegate { try { await _semaphore.WaitAsync().ConfigureAwait(false); //await _logsFolder.DeleteAsync().AsTask().ConfigureAwait(false); _logsFolder = await ApplicationData.Current.LocalCacheFolder.CreateFolderAsync(LogFolderName, CreationCollisionOption.ReplaceExisting).AsTask().ConfigureAwait(false); } catch (Exception exc) { if (SemaphoreSlimSafeRelease.IsAlive(_semaphore)) { Debug.WriteLine("ERROR in Logger.ClearAll(): " + exc.ToString()); } } finally { SemaphoreSlimSafeRelease.TryRelease(_semaphore); } }); }
private static async Task Add2Async(string msg, string fileName) { try { await _semaphore.WaitAsync().ConfigureAwait(false); await UpdateLogAsync(fileName, msg).ConfigureAwait(false); //Debug.WriteLine("the thread id is " + Environment.CurrentManagedThreadId + " after the await"); } catch (Exception exc) { if (SemaphoreSlimSafeRelease.IsAlive(_semaphore)) { Debug.WriteLine("ERROR in Logger: " + exc.ToString()); } } finally { SemaphoreSlimSafeRelease.TryRelease(_semaphore); } // await SendEmailWithLogsAsync("*****@*****.**"); // maybe move Logger into the utils and use the right email address and maybe new parameter "sendemailifcrash" // On second thought, the email could be annoying and scary. Better leave the option to send it in the "About" panel only. }
/// <summary> /// Disposes of a semaphore. /// You can use this method to interrupt all operations, which are queuing at the semaphore, if you initialised it for max 1 thread. /// Wait at the semaphore before calling this method, to be sure you don't disrupt any running operations at an unexpected moment. /// Every method using this semaphore should make allowance for it being disposed. /// </summary> /// <example> /// Use it as follows: /// <code> /// try /// { /// semaphore.Wait(); /// do some work /// } /// catch (Exception ex) /// { /// if (SemaphoreSlimSafeRelease.IsAlive(semaphore)) /// // you have a real error, otherwise it's simply the semaphore being disposed and reset to null /// } /// finally /// { /// SemaphoreSlimSafeRelease.TryRelease(semaphore); /// // do not just call semaphore.Release() because the semaphore may be disposed or null at this stage /// } /// </code> /// </example> /// <param name="semaphore"></param> /// <returns></returns> public static bool TryDispose(SemaphoreSlimSafeRelease semaphore) { try { if (IsAlive(semaphore)) { semaphore.Dispose(); return true; } } catch { } return false; }
/// <summary> /// Returns true if a SemaphoreSlimSafeRelease is not null and not disposed. /// </summary> /// <param name="semaphore"></param> /// <returns></returns> public static bool IsAlive(SemaphoreSlimSafeRelease semaphore) { return semaphore != null && !semaphore._isDisposed; }
public DocumentView() { _appView = ApplicationView.GetForCurrentView(); _appView.VisibleBoundsChanged += OnVisibleBoundsChanged; _renderSemaphore = new SemaphoreSlimSafeRelease(1, 1); Loaded += OnLoaded; Unloaded += OnUnloaded; InitializeComponent(); }
public Task<bool> RecordAsync(StorageFile file, CancellationToken cancToken) { return RunFunctionIfOpenAsyncTB(async delegate { _file = file; await SetupUiAsync(); bool isOk = await InitializeCameraAsync().ConfigureAwait(false); if (isOk) { _triggerSemaphore = new SemaphoreSlimSafeRelease(0, 1); // this semaphore is always closed at the beginning try { await _triggerSemaphore.WaitAsync(cancToken).ConfigureAwait(false); } catch (OperationCanceledException) { } catch (Exception ex) { if (SemaphoreSlimSafeRelease.IsAlive(_triggerSemaphore)) { Logger.Add_TPL(ex.ToString(), Logger.ForegroundLogFilename); } } } //else //{ // Task beginFailureAnim = RunInUiThreadAsync(delegate { FailureStoryboard.Begin(); }); //} return isOk; }); }
/// <summary> /// I need this override to stop any running media recording /// </summary> /// <returns></returns> //public override async Task<bool> CloseAsync() // LOLLO fix this like in AudioRecorderView if you want to use this class //{ // if (!_isOpen) return false; // SemaphoreSlimSafeRelease.TryRelease(_triggerSemaphore); // return await base.CloseAsync().ConfigureAwait(false); //} protected override async Task CloseMayOverrideAsync() { await Stop2Async().ConfigureAwait(false); SemaphoreSlimSafeRelease.TryDispose(_triggerSemaphore); _triggerSemaphore = null; }
/// <summary> /// Returns true if a SemaphoreSlimSafeRelease is not null and not disposed. /// </summary> /// <param name="semaphore"></param> /// <returns></returns> public static bool IsAlive(SemaphoreSlimSafeRelease semaphore) { return(semaphore != null && !semaphore._isDisposed); }
protected override async Task CloseMayOverrideAsync() { await StopRecordingAsync().ConfigureAwait(false); SemaphoreSlimSafeRelease.TryDispose(_recordLockingSemaphore); _recordLockingSemaphore = null; var mc = _mediaCapture; if (mc != null) { mc.Failed -= OnMediaCapture_Failed; try { mc.Dispose(); } catch { } } _mediaCapture = null; var audioRecorder = _audioRecorder; if (audioRecorder != null) { audioRecorder.UnrecoverableError -= OnAudioRecorder_UnrecoverableError; await audioRecorder.CloseAsync(); } _audioRecorder = null; Task stopAllAnims = RunInUiThreadAsync(delegate { RecordingStoryboard.SkipToFill(); RecordingStoryboard.Stop(); FailureStoryboard.SkipToFill(); FailureStoryboard.Stop(); }); }