解决方案 »

  1.   

    希望对你有帮助。
    呵呵。using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Diagnostics;
    using System.ServiceProcess;
    using System.Text;
    using System.IO;
    using System.Timers;namespace TickService
    {
        public partial class Service1 : ServiceBase
        {
            Timer timer1;        public Service1()
            {
                InitializeComponent();
                timer1 = new Timer();
                //Set the interval of timer to 3 seconds.
                timer1.Interval = 3000;
                //Enable the timer.
                timer1.Enabled = true;
                timer1.Elapsed+=new ElapsedEventHandler(timer1_Elapsed);
            }        protected override void OnStart(string[] args)
            {
                // TODO: Add code here to start your service.
                
                //Append the text to the sample file.
                StreamWriter writer = File.AppendText(@"C:\sample.txt");
                writer.WriteLine("Start");
                writer.Close();
            }        protected override void OnStop()
            {
                // TODO: Add code here to perform any tear-down necessary to stop your service.
                //Append the text Stop to the C:\sample.txt file.
                StreamWriter writer = File.AppendText(@"C:\sample.txt");
                writer.WriteLine("Stop");
                writer.Close();
            }        private void timer1_Elapsed(object sender, EventArgs e)
            {
                //Set the enable property to false.
                //timer1.Enabled = false;
                //Append the text Tick to the C:\sample.txt file.
                StreamWriter writer = File.AppendText(@"C:\sample.txt");
                writer.WriteLine("Tick");
                writer.Close();
                //timer1.Enabled = true;
            }
        }
    }
      

  2.   

    线程会执行多久,那要看你想让它执行多久.
    可能执行一次就退出,也可能一直循环执行,一直到天荒地老,服务器崩溃你设置了2个timer,回调时间一个1小时,另一个1分钟
    你想知道它到底有没有运行,写配置文件啊,然后看文件里的信息
    光从结果上看,你第一次已经把文件都处理完了,1分钟后没有可以处理的文件,当然就不再执行了