zoukankan      html  css  js  c++  java
  • C# 可取消的延时实现

    方式1:

    private void button2_Click(object sender, EventArgs e)
            {
                m_th = new Thread(new ThreadStart(CheckDogJob));
                m_th.IsBackground = true;
                m_th.Start();
            }
    
            private async void CheckDogJob()
            {
                try
                {
                    Console.Write("start----");
                    await Task.Delay(3000000, m_cts.Token);
                   
                    Console.Write("End----");
                }
                catch (OperationCanceledException)
                {
                    Console.Write("Cancel----");
                }
            }
    
            private void button3_Click(object sender, EventArgs e)
            {
                m_cts.Cancel();
            }

    方式二:

     private void button2_Click(object sender, EventArgs e)
            {
                m_th = new Thread(new ThreadStart(CheckDogJob));
                m_th.IsBackground = true;
                m_th.Start();
            }
    
            private void CheckDogJob()
            {
                Console.Write("start----");
                Task.Delay(3000000, m_cts.Token).ContinueWith((t) =>
                {
                    Console.Write("Cancel----");
                }, TaskContinuationOptions.OnlyOnCanceled).Wait();
                Console.Write("End----");
            }
    
            private void button3_Click(object sender, EventArgs e)
            {
                m_cts.Cancel();
            }

    方式三

      private AutoResetEvent m_startDelayEvent = new AutoResetEvent(false);
    
    priviate void Test()
    {
        m_startDelayEvent.WaitOne(100000);
    }
    
    
    //取消
      m_startDelayEvent.Set();
  • 相关阅读:
    JSP详细解析
    JSP详细解析
    JAVA设计模式之单例模式
    JAVA设计模式之单例模式
    SQLite – GLOB子句
    HEXO进阶打赏
    python常用模块
    猫头鹰的深夜翻译:核心JAVA并发一
    标准规范
    题解 P1951 【收费站_NOI导刊2009提高(2)】
  • 原文地址:https://www.cnblogs.com/karl-F/p/8793779.html
Copyright © 2011-2022 走看看