为什么我写的程序timer中不会自动执行呢?
     
    用的是
     this.time1.Tick+=new System.EventHandler(this.time1_Tick);
     这边写调用的方法:
     private boid time1_Tick(object sender,EventArgs e)
{
}      time1 的属性已经设置为True了.
     请大家赐教,谢谢...

解决方案 »

  1.   

    用System.Timers.Timer好点。//Timer类
    using System;namespace CodeGuru.TimerService
    {
       /// <summary>
       /// Demonstrate the use of a timer.
       /// </summary>
       public class TimedItem
       {
          private System.Diagnostics.EventLog _AppEventLog;
          private System.Timers.Timer _Timer;      private int _Interval = 30000;
          public int Interval
          {
             get { return this._Interval; }
             set { this._Interval = value; }
          }      private string _Name = "";
          public string Name
          {
             get { return this._Name; }
             set { this._Name = value; }
          }      /// <summary>
          /// Constructor
          /// </summary>
          public TimedItem(System.Diagnostics.EventLog AppEventLog)
          {
             this._Timer = new System.Timers.Timer();
             this._Timer.Elapsed += new
             System.Timers.ElapsedEventHandler(_Timer_Elapsed);
             this._Timer.Enabled = false;
             this._Timer.Interval = this.Interval;
             this._AppEventLog = AppEventLog;
          }      /// <summary>
          /// Start the timer.
          /// </summary>
          public void StartTimer()
          {
             this._Timer.Enabled = true;
          }      /// <summary>
          /// Stop the timer.
          /// </summary>
          public void StopTimer()
          {
             this._Timer.Enabled = false;
          }
          /*
           * Respond to the _Timer elapsed event.
           */
          private void _Timer_Elapsed(object sender,
             System.Timers.ElapsedEventArgs e)
          {
             this._AppEventLog.WriteEntry("Time elapsed for " + this.Name,
                System.Diagnostics.EventLogEntryType.Information);
          }
       }
    }//windows service 类using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Diagnostics;
    using System.ServiceProcess;
    using System.Threading;namespace CodeGuru.TimerService
    {
       public class TimerService : System.ServiceProcess.ServiceBase
       {
          private System.Diagnostics.EventLog _AppEventLog;
          private Thread[] _WorkerThreads;
          private TimedItem[] _Timers;      /// <summary>
          /// Required designer variable.
          /// </summary>
          private System.ComponentModel.Container components = null;      public TimerService()
          {
             // This call is required by the Windows.Forms Component
             // Designer.
             InitializeComponent();         // TODO: Add any initialization after the InitComponent
             // call
          }      // The main entry point for the process
          static void Main()
          {
             System.ServiceProcess.ServiceBase[] ServicesToRun;
             ServicesToRun = new System.ServiceProcess.ServiceBase[] { 
                new TimerService() };         System.ServiceProcess.ServiceBase.Run(ServicesToRun);
          }      /// <summary>
          /// Required method for Designer support - do not modify
          /// the contents of this method with the code editor.
          /// </summary>
          private void InitializeComponent()
          {
             this._AppEventLog = new System.Diagnostics.EventLog();
                ((System.ComponentModel.ISupportInitialize)
                 (this._AppEventLog)).BeginInit();
             //
             // _AppEventLog
             //
             this._AppEventLog.Log = "Application";
             this._AppEventLog.Source = "TimerService";
             //
             // TimerService
             //
             this.ServiceName = "TimerService";
                ((System.ComponentModel.ISupportInitialize)
                 (this._AppEventLog)).EndInit();
          }      /// <summary>
          /// Clean up any resources being used.
          /// </summary>
          protected override void Dispose( bool disposing )
          {
             if( disposing )
             {
                if (components != null)
                {
                   components.Dispose();
                }
             }
             base.Dispose( disposing );
          }      /// <summary>
          /// Set things in motion so your service can do its work.
          /// </summary>
          protected override void OnStart(string[] args)
          {
             this._Timers = new TimedItem[5];
             _WorkerThreads = new Thread[5];         for( int i = 0; i < 5; i++ )
             {
                _Timers[i] = new TimedItem(this._AppEventLog);
                _Timers[i].Name = "Timer #" + i.ToString();
                _WorkerThreads[i] = new Thread(new
                   System.Threading.ThreadStart(_Timers[i].StartTimer));
                _WorkerThreads[i].Start();
             }
          }      /// <summary>
          /// Stop this service.
          /// </summary>
          protected override void OnStop()
          {
             for( int i = 0; i < 5; i++ )
             {
                _Timers[i].StopTimer();
             }
          }
       }
    }
    参考:
    http://www.codeguru.com/columns/dotnet/article.php/c6919__1/Using-Timers-in-a-Windows-Service.htm
      

  2.   

    Timer控件有2种,你是否选择正确了?
      

  3.   

    要用System.Timers.Timer
    而不是System.Window.Form.Timer
      

  4.   

    控件serviceInstaller1属性中有个starttype选为automatic
      

  5.   

    this.time1.Tick+=new System.EventHandler(this.time1_Tick);这是写在哪里的?