예제 #1
0
 public static Task CloseWithTimeout(IMyStream stream, int timeout = -2)
 {
     if (stream == null)
     {
         throw new ArgumentNullException(nameof(stream));
     }
     if (timeout < -2)
     {
         throw new ArgumentOutOfRangeException(nameof(timeout), "should be -2 (default), -1 (infinity), or >= 0.");
     }
     if (timeout == -2)
     {
         timeout = 10 * 1000;
     }
     if (stream is IMyStreamDispose)
     {
         if (stream.State.IsDisposed)
         {
             return(NaiveUtils.CompletedTask);
         }
     }
     else if (stream.State.IsClosed)
     {
         return(NaiveUtils.CompletedTask);
     }
     return(Task.Run(async() => {
         try {
             var closeTask = stream is IMyStreamDispose dis ? dis.Dispose() : stream.Close();
             if (await closeTask.WithTimeout(timeout))
             {
                 Logging.warning($"stream closing timed out ({timeout} ms). ({stream})");
             }
             await closeTask.CAF();
         } catch (Exception e) {
             Logging.exception(e, Logging.Level.Warning, $"stream closing ({stream.SafeToStr()})");
         }
     }));
 }