Ejemplo n.º 1
0
        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);
            }
        }
Ejemplo n.º 2
0
 public static string MyNameOfMethod <T1>(
     this T1 obj,
     Expression <Action <T1> > expression
     )
 {
     return(MyNameof <T1> .MethodName(expression));
 }
Ejemplo n.º 3
0
 public static void GetRandomBytes(byte[] bArray)
 {
     if (bArray == null)
     {
         throw new ArgumentNullException(
                   MyNameof.GetLocalVarName(() => bArray));
     }
     Rng.GetBytes(bArray);
 }
Ejemplo n.º 4
0
 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());
     }
 }
Ejemplo n.º 5
0
 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;
 }
Ejemplo n.º 6
0
 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);
        }
Ejemplo n.º 8
0
 public static string MyNameOfProperty <T1, TProp>(
     this T1 obj,
     Expression <Func <T1, TProp> > expression)
 {
     return(MyNameof <T1> .Property(expression));
 }