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();
  • 相关阅读:
    oracle学习笔记(十五) PL/SQL语法结构以及使用
    Jquery1
    DOM2
    DOM
    JS的使用
    登录
    数据库操作是sql的操作1
    数据库2_sqlHelper
    数据库1数据库常用指令
    C# 基础
  • 原文地址:https://www.cnblogs.com/karl-F/p/8793779.html
Copyright © 2011-2022 走看看