public static async Task <bool> WaitForProcessExitAsync( Process p, CancellationToken token, int milliseconds ) { if (p == null) { throw new ArgumentNullException( MyNameof.GetLocalVarName(() => p)); } var tDelay = Task.Delay(milliseconds); while (true) { if (p.HasExited) { return(true); } if (tDelay.IsCompleted) { return(false); } if (token.IsCancellationRequested) { return(false); } await Task.Delay(50).ConfigureAwait(false); } }
public static string MyNameOfMethod <T1>( this T1 obj, Expression <Action <T1> > expression ) { return(MyNameof <T1> .MethodName(expression)); }
public static void GetRandomBytes(byte[] bArray) { if (bArray == null) { throw new ArgumentNullException( MyNameof.GetLocalVarName(() => bArray)); } Rng.GetBytes(bArray); }
public static byte[] Compress(byte[] raw) { if (raw == null) { throw new ArgumentNullException( MyNameof.GetLocalVarName(() => raw) ); } using (var msWrapper = MemoryStreamPool.DefaultPool.GetStreamWrapper()) { var msWrapperStream = msWrapper.MStream; using (var gzip = new GZipStream(msWrapperStream, CompressionMode.Compress, true)) { gzip.Write(raw, 0, raw.Length); } return(msWrapperStream.ToArray()); } }
public MemoryStreamWrapper( NoClosingMemoryStream ms, ConcurrentQueue <NoClosingMemoryStream> addOnExit ) { if (ms == null) { throw new ArgumentNullException( MyNameof.GetLocalVarName(() => ms)); } if (addOnExit == null) { throw new ArgumentNullException( MyNameof.GetLocalVarName(() => addOnExit)); } _addOnExit = addOnExit; _ms = ms; }
public static byte[] Decompress(byte[] gzip) { if (gzip == null) { throw new ArgumentNullException( MyNameof.GetLocalVarName(() => gzip) ); } using ( var gzipMsWrapper = MemoryStreamPool.DefaultPool.GetStreamWrapper(gzip) ) { var gzipMs = gzipMsWrapper.MStream; using ( var stream = new GZipStream( gzipMs, CompressionMode.Decompress, true ) ) { const int size = 4096; var buffer = new byte[size]; using (var msWrapper = MemoryStreamPool.DefaultPool.GetStreamWrapper()) { var msWrapperStream = msWrapper.MStream; int count = 0; do { count = stream.Read(buffer, 0, size); if (count > 0) { msWrapperStream.Write(buffer, 0, count); } } while (count > 0); return(msWrapperStream.ToArray()); } } } }
public static async Task <SemaphoreSlimDisposableWrapper> CreateInstance( SemaphoreSlim lockSem, string mthdName, CancellationToken token ) { if (lockSem == null) { throw new ArgumentNullException( MyNameof.GetLocalVarName(() => lockSem) ); } var wrapperNum = Interlocked.Increment(ref _nextDbNum); var result = new SemaphoreSlimDisposableWrapper(lockSem); result._wrapperNum = wrapperNum; result.MthdName = mthdName; result.TryEnterTime = DateTime.UtcNow; SemWrappersDb.TryAdd(wrapperNum, result); await result._lockSem.WaitAsync(token).ConfigureAwait(false); result.EnterTime = DateTime.UtcNow; return(result); }
public static string MyNameOfProperty <T1, TProp>( this T1 obj, Expression <Func <T1, TProp> > expression) { return(MyNameof <T1> .Property(expression)); }