zoukankan      html  css  js  c++  java
  • Function with Timeout

      /// <summary>
            /// if one drive broken, use [Directory.Exists] may cause 10 seconds,
            /// so design this function with timeout.
            /// </summary>
            /// <param name="path"></param>
            /// <param name="timeout"></param>
            /// <returns></returns>
            public static bool? CheckPathExistWithTimeout(string path, double timeout)
            {
                bool isWait = true;
                bool? isReady = null;
                CancellationTokenSource cancelSource = new CancellationTokenSource();
                Task.Factory.StartNew(() =>
                {
                    isReady = Directory.Exists(path);
                    isWait = false;
                }, cancelSource.Token);
    
                int ticks = 0;
                while (isWait)
                {
                    if (ticks >= timeout * 1000)
                    {
                        break;
                    }
                    ticks++;
                    Thread.Sleep(TimeSpan.FromMilliseconds(1));
                }
                cancelSource.Cancel();
                return isReady;
            }
  • 相关阅读:
    OpenCV鼠标事件
    相对复杂一些的爬虫,针对拒绝爬虫的网站
    CSS
    JS
    JS
    JS
    JS
    TypeScript
    微信小程序
    微信小程序
  • 原文地址:https://www.cnblogs.com/xiaokang088/p/2758803.html
Copyright © 2011-2022 走看看