前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >c# 使用timer定时器操作,上次定时到了以后,下次还未执行完怎么处理

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

作者头像
跟着阿笨一起玩NET
发布2018-09-18 16:15:45
3.7K0
发布2018-09-18 16:15:45
举报
文章被收录于专栏:跟着阿笨一起玩NET

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

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

代码语言:javascript
复制
定时器定时执行某一个方法时,可能由于执行的时间长要比间隔的时间长,则这种情况可能导致线程并发性的问题。建议加上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);
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2013-05-05 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档