c# 使用timer定時器操作,上次定時到了以后,下次還未執行完怎么處理


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);
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM