大家好!
最近我写了一个windows 服务程序,服务定时连接服务器数据库,如果有新的版本就会立即通知用户下载最新版软件。我在服务中加了Timer控件,好像不起作用。各位大侠有什么高招不防介绍一下,如果有代码参考更好,多谢!

解决方案 »

  1.   

    用系统的Timer或者直接用个线程吧...我们有个同事曾经做过,他用的是线程...
      

  2.   

    建议用线程
    参考:
    http://www.hnce.net/Content/slick.238.html
      

  3.   

    Timer控件是用于窗口的,而且不是很准确,服务程序请用系统的Timer类
      

  4.   

    /// <summary>
     /// 在指定的时间唤醒指定的程序
     /// </summary>
     public class Planner:IDisposable
     {
      private GOA.Interface.IPlanalbe _plan = null;
      private string tag;
      private DateTime settingTime;
      private System.Timers.Timer timer = null;
      /// <summary>
      /// 唤醒器
      /// </summary>
      /// <param name="timeExpression">时间表达式,定义在什么时候唤醒</param>
      /// <param name="plan">需要唤醒的程序</param>
      public Planner(string timeExpression , GOA.Interface.IPlanalbe plan)
      {
       tag = timeExpression.Trim();
       _plan = plan;
       InitTimer();
      }
      private void InitTimer()
      {
       timer = new System.Timers.Timer(200);
       SetNotifyTime(DateTime.Now);
       timer.Elapsed +=new System.Timers.ElapsedEventHandler(timer_Elapsed);
       timer.AutoReset = true;
       timer.Start();
      }
      private void SetNotifyTime(DateTime dt)
      {
       string[] t = tag.Split(':');
       //System.Windows.Forms.MessageBox.Show(this.tag);
       int hour = int.Parse(t[0]);
       int min = int.Parse(t[1]);   settingTime = new DateTime(dt.Year,dt.Month,dt.Day,hour,min,0);
       if(settingTime < DateTime.Now)
       {
        settingTime = settingTime.AddDays(1);
       }
      }  private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
      {
       if(settingTime.ToShortDateString() == DateTime.Now.ToShortDateString() && settingTime.ToShortTimeString() == DateTime.Now.ToShortTimeString())
       {
        try
        {
         Utils.SystemUtil.DebugLog("Planner::timer_Elapsed::Begin");
         timer.AutoReset = false;
         timer.Stop();
         Utils.SystemUtil.DebugLog("Planner::timer_Elapsed::1");
         _plan.Action();
         Utils.SystemUtil.DebugLog("Planner::timer_Elapsed::2");
        }
        finally
        {
         SetNotifyTime(DateTime.Now.AddDays(1));
         timer.AutoReset = true;
         timer.Start();
        }
        Utils.SystemUtil.DebugLog("Planner::timer_Elapsed::End");
       }
      }
      #region IDisposable 成员  public void Dispose()
      {
       Utils.SystemUtil.DebugLog("Planner::Dispose::Begin");
       if(timer != null)
       {
        Utils.SystemUtil.DebugLog("Planner::Dispose::IN");    timer.Stop();
        timer.Dispose();
       }
       Utils.SystemUtil.DebugLog("Planner::Dispose::End");  }  #endregion
     }
      

  5.   

    using System;
    using System.Threading;
    using System.Timers;public class WaitableTimerSample
    {
        public static void Main(String[] args)
        {
            System.Timers.Timer aTimer = new System.Timers.Timer();        aTimer.Elapsed += new ElapsedEventHandler(OnTimer);        aTimer.Interval = 1000;
            aTimer.Enabled = true;        Console.WriteLine("按“q”退出该示例");
            while(Console.Read()!='q');
        }    public static void OnTimer(Object source, ElapsedEventArgs e)
        {
            Console.WriteLine("Hello World!");
        }
    }
    Imports System
    Imports System.Threading
    Imports System.TimersNamespace Timer    Module Timer        Public Sub Main()
                Dim aTimer As System.Timers.Timer
                aTimer = New System.Timers.Timer            AddHandler aTimer.Elapsed, AddressOf OnTimer            aTimer.Interval = 1000
                aTimer.Enabled = True            Console.WriteLine("按""q""退出该示例")
                While (Console.Read() <> 113)
                End While
            End Sub        Public Sub OnTimer(ByVal source As Object, ByVal e As ElapsedEventArgs)
                Console.WriteLine("Hello World!")
            End Sub    End ModuleEnd Namespace
      

  6.   

    public partial class MyService : ServiceBase
        {   private System.Timers.Timer timer1;
       /// <res> 
       /// Required designer variable.
       /// </res>
       private System.ComponentModel.Container components = null;   public MyService()
       {
           // This call is required by the Windows.Forms 
           // Component Designer.
         InitializeComponent();
       }   // The main entry point for the process
       static void Main()
       {
         System.ServiceProcess.ServiceBase[] ServicesToRun;
       
         ServicesToRun = new System.ServiceProcess.ServiceBase[] 
    { new MyService() };     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.timer1 = new System.Timers.Timer();
         ((System.ComponentModel.ISupportInitialize)
    (this.timer1)).BeginInit();
         // 
         // timer1
         // 
         this.timer1.Interval = 30000;
         this.timer1.Elapsed += 
       new System.Timers.ElapsedEventHandler(this.timer1_Elapsed);
         // 
         // MyService
         // 
         this.ServiceName = "My Sample Service";
         ((System.ComponentModel.ISupportInitialize)
    (this.timer1)).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.timer1.Enabled = true;
       }
     
       /// <summary>
       /// Stop this service.
       /// </summary>
       protected override void OnStop()
       {
         this.timer1.Enabled = false;
       }    private void timer1_Elapsed(object sender, 
    System.Timers.ElapsedEventArgs e)
       {
          //
       }   }
      

  7.   

    用系统的Timer或者直接用个线程
      

  8.   

    启动的时候,去检查一下就行,为什么要用timer,
      

  9.   

    大家顺便讨论下Microsoft的Windows自动升级是怎么实现的,是如何推给用户的?
      

  10.   

    来晚了 Timer\ 中的IterVal 方法 指定时间执行
    或者是  System.Threading.Thread
      

  11.   

    好帖!
    希望大家讨论下Microsoft的Windows自动升级是怎么实现的,是如何推给用户的