c# 使用timer定时器操作,下次定时到了以后,上次还未执行完怎么办

------解决方案--------------------------------------------------------
开始的时候,禁用定时器,你可以在执行完毕之后再启用定时器

定时器定时执行某一个方法时,可能由于执行的时间长要比间隔的时间长,则这种情况可能导致线程并发性的问题。建议加上Lock
private static object LockObject = new Object();
private static void CheckUpdatetimer_Elapsed(object sender, ElapsedEventArgs e)
{
    // 加锁检查更新锁
   lock (LockObject)
   {
   }
}
// From command line, compile with /r:System.dll
using System;
using System.Timers;
using System.Threading;

public class Timer2
{
    //static System.Windows.Forms.Timer aTimer = new System.Windows.Forms.Timer();

private static System.Timers.Timer aTimer;
    static object o = new object();
    public static void Main()
    {
        // Normally, the timer is declared at the class level,
        // so that it stays in scope as long as it is needed.
        // If the timer is declared in a long-running method,  
        // KeepAlive must be used to prevent the JIT compiler 
        // from allowing aggressive garbage collection to occur 
        // before the method ends. (See end of method.)
        //System.Timers.Timer aTimer;

// Create a timer with a ten second interval.
        aTimer = new System.Timers.Timer(2000);
        aTimer.Enabled = true;

// Hook up the event handler for the Elapsed event.
        aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);

// Only raise the event the first time Interval elapses.
        aTimer.AutoReset = true;

Console.WriteLine("Press the Enter key to exit the program.");
        Console.ReadLine();

// If the timer is declared in a long-running method, use
        // KeepAlive to prevent garbage collection from occurring
        // before the method ends.
        //GC.KeepAlive(aTimer);
    }

// Specify what you want to happen when the Elapsed event is 
    // raised.
    static object name=0;
    private static void OnTimedEvent(object source, ElapsedEventArgs e)
    {
        //lock (o)
        {
            aTimer.Enabled = false;
            aTimer.Interval = 1000;
            lock(name)
            {
                name = Convert.ToInt32(name) + 1;
                Thread.CurrentThread.Name = name.ToString();
            }
            Console.WriteLine(DateTime.Now.ToString()+"-"+Thread.CurrentThread.Name);
            //Thread.Sleep(1000000);
            Waste();
            aTimer.Enabled = true; 
        }

}

/// <summary>
    /// 模拟长时间的操作
    /// </summary>
    public static void Waste()
    {
        for (int i = 0; i < Int32.MaxValue; i++)
        {
            
        }
        //Thread.Sleep(10000);
        Console.WriteLine(DateTime.Now.ToString()+"完成-"+Thread.CurrentThread.Name);
    }
}

转载于:https://www.cnblogs.com/51net/archive/2013/05/05/3061231.html

c# 使用timer定时器操作,上次定时到了以后,下次还未执行完怎么处理相关推荐

  1. VS Code中Python操作办公软件Excel(待续,还未写完)

    1.Python操作Excel读取 1.1.Excel基本内容 工作簿(workbook) 表单 (worksheet) 行.列.单元格(row,column,cell) 1.2.安装openpyxl ...

  2. JS定时器使用,定时定点,固定时刻,循环执行

    本文概述:本文主要介绍通过JS实现定时定点执行,在某一个固定时刻执行某个函数的方法.比如说在下一个整点执行,在每一个整点执行,每隔10分钟定时执行的方法. JavaScript中有两个定时器方法:se ...

  3. Java 中Timer定时器设置订单提交后24小时未付款订单状态为已关闭。

    1. 简单的Timer定时器方法 public class CommTimer {/*** 设置指定24小时后执行*/public static void orderClose() {final Ti ...

  4. 定时线程的使用 java_Java线程Timer定时器用法详细总结

    定时/计划功能主要使用的就是Timer对象,它在内部还是使用多线程的方式进行处理,所以它和线程技术还是有非常大的关联. Timer类主要作用就是设置计划任务,但封装任务的类却是TimerTask类.T ...

  5. ASP.NET通过Global.asax和Timer定时器定时运行后台代码

    ASP.NET通过Global.asax和Timer定时器定时运行后台代码 Global.asax文件 1.Application_Start方法添加     // 在应用程序启动时运行的代码     ...

  6. Apollo 笔记(03)— Cyber RT Python 接口(channel 读和写、server/client 通信、record 文件读写信息查询、timer 时间定时器操作)

    https://cyber-rt.readthedocs.io/en/latest/CyberRT_Python_API.html https://cyber-rt.readthedocs.io/en ...

  7. JDK中Timer定时器底层到底是怎么实现的?

    关于定时器在JDK中提供了两种方式,一种是 Timer(同步调度) ,另一种是ScheduledThreadPoolExecutor(异步调度). 更高级的任务调度开源框架就比较多了,但最核心的定时器 ...

  8. java Timer定时器管理类

    1.java timer类,定时器类.启动执行定时任务方法是timer.schedule(new RemindTask(), seconds*1000);俩参数分别是TimerTask子类,具体执行定 ...

  9. setitimer 创建两个定时器_UE4 Timer(定时器)相关源码分析

    前言 文章属于旧有文章搬运, 之前在csdn上面 2019.11.6修改 Timer是定时器, 用途: 延时执行事件 周期性执行事件 参考官方文档Using Timers Timer可以... Tim ...

最新文章

  1. loj 2542 随机游走 —— 最值反演+树上期望DP+fmt
  2. curl-loader介绍
  3. 雷军:有人说我写的代码像诗一样优雅~
  4. c++ qt获取电脑的内存_Qt官方示例信号量
  5. L1-043 阅览室-PAT团体程序设计天梯赛GPLT
  6. 中国网络空间安全协会成立 永信至诚等200家单位共建网络安全
  7. 【android自定义控件】LinearLayout定义ActionBar样式
  8. AxureUX 复制Iconfont图标到Axure
  9. lg相乘公式_ln公式(lg公式大全)
  10. js 取表格table td值 botton a
  11. 电脑搜索不到wifi信号的问题解决
  12. 腾讯云学生服务器官网地址(附用途指南)
  13. java des ecb_java DES ECB模式对称加密解密
  14. 计算机毕设结束语致谢,毕业设计结束语和致谢
  15. 【库房】DateTime.Now.Date和DateTime.Now.TimeOfDay的用法
  16. CEC循环生态社区周而复始生生不息推进XAG令牌市场
  17. linux grant命令,linux指令集
  18. lammps计算团簇的方法
  19. 网易微专业python爬虫工程师_ai工程师 自然语言处理
  20. 专科出身,苦学Android,最终圆梦阿里,2021最新Android框架体系架构面试题

热门文章

  1. 改善深层神经网络:超参数调整、正则化以及优化——2.7 RMSprop
  2. 数字图像处理--图像梯度的基本原理
  3. FFmpeg命令的几种应用
  4. mysql的root密码的变更-进程的杀掉
  5. 计算机软件通常分为两大类 它们是,计算机考试题库:计算机考试练习题(79)...
  6. notepad自动对齐html代码,notepad如何存储为html格式化
  7. 通达信缠论三买三卖指标公式_【新增选股指标:中枢突破】通达信版缠论自动画线选股指标今天更新1个重要新版...
  8. Windows 10 开启有线无线802.1x脚本
  9. python 元组是什么_python元组是什么?python元组的用法介绍
  10. android 网络编程面试题,Android面试题整理