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();
  • 相关阅读:
    简单好用的日历排期控件
    Ext.js create store
    Ext.js页面添加元素
    Ext.js Tree
    前端设计的七大法则
    如何写软件开发相关文档,它包含哪些种类和内容
    行内文字末尾下降
    正则表达式
    滚动加载数据
    location.hash来保持页面状态
  • 原文地址:https://www.cnblogs.com/karl-F/p/8793779.html
Copyright © 2011-2022 走看看