C# 在计时器不工作的情况下,每x分钟重新运行一次服务

C# 在计时器不工作的情况下,每x分钟重新运行一次服务,c#,windows-services,C#,Windows Services,我有一个服务,我想使用(例如)计时器每X分钟运行一次 这不起作用,为什么?有更好的办法吗?尝试搜索,但没有找到任何适合我的方法…断点从未命中顶部方法 static void Main() { WriteLine("service has started"); timer = new Timer(); timer.Enabled = true; timer.Interval = 1000; timer.Auto

我有一个服务,我想使用(例如)计时器每X分钟运行一次

这不起作用,为什么?有更好的办法吗?尝试搜索,但没有找到任何适合我的方法…断点从未命中顶部方法

static void Main()
    {
        WriteLine("service has started");
        timer = new Timer();
        timer.Enabled = true;
        timer.Interval = 1000;
        timer.AutoReset = true;
        timer.Start();
        timer.Elapsed += scheduleTimer_Elapsed;
    }

    private static void scheduleTimer_Elapsed(object sender, ElapsedEventArgs e)
    {
        WriteLine("service is runs again");
    }

    public static void WriteLine(string line)
    {
        Console.WriteLine(line);
    }

我之前也遇到过同样的情况。我使用了下面的代码,它对我有效

// The main Program that invokes the service   
static class Program
{

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    static void Main()
    {
        ServiceBase[] ServicesToRun;
        ServicesToRun = new ServiceBase[] 
        { 
            new Service1() 
        };
        ServiceBase.Run(ServicesToRun);
    }
}


//Now the actual service

public partial class Service1 : ServiceBase
{
    public Service1()
    {
        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {
        ///Some stuff

        RunProgram();

        ///////////// Timer initialization
        var scheduleTimer = new System.Timers.Timer();
        scheduleTimer.Enabled = true;
        scheduleTimer.Interval = 1000;
        scheduleTimer.AutoReset = true;
        scheduleTimer.Start();
        scheduleTimer.Elapsed += new ElapsedEventHandler(scheduleTimer_Elapsed);
    }

    protected override void OnStop()
    {
    }

    void scheduleTimer_Elapsed(object sender, ElapsedEventArgs e)
    {
        RunProgram();
    }

    //This is where your actual code that has to be executed multiple times is placed
    void RunProgram()
    {
     //Do some stuff
    }
}
//调用服务的主程序
静态类程序
{
/// 
///应用程序的主要入口点。
/// 
静态void Main()
{
ServiceBase[]ServicesToRun;
ServicesToRun=新的ServiceBase[]
{ 
新服务1()
};
ServiceBase.Run(ServicesToRun);
}
}
//现在是实际服务
公共部分类Service1:ServiceBase
{
公共服务1()
{
初始化组件();
}
启动时受保护的覆盖无效(字符串[]args)
{
///一些东西
运行程序();
/////////////定时器初始化
var scheduleTimer=new System.Timers.Timer();
scheduleTimer.Enabled=true;
scheduleTimer.Interval=1000;
scheduleTimer.AutoReset=true;
scheduleTimer.Start();
scheduleTimer.Appeased+=新的ElapsedEventHandler(scheduleTimer_Appeased);
}
受保护的覆盖void OnStop()
{
}
已过期的无效计划计时器(对象发送器,ElapsedEventArgs e)
{
运行程序();
}
//这是必须多次执行的实际代码所在的位置
void RunProgram()
{
//做点什么
}
}

在运行服务之前启动计时器?不确定,但是在
timer.appeased+=…
语句之后添加
timer.Enabled=True
,而不是在它之前添加
timer.Start()
?读取线程计时器,这样就不会调用Main方法了?你又给RunProgram打电话了?但是您不需要调用Main吗?因为已经设置了计时器?当服务第一次运行时,仍然会调用Main方法。然后我们需要重新运行RunProgramHmm..用这段代码,我的scheduleTimer_Exposed永远不会被调用,为什么当我在VS中按“start”时,它在Main()之后就消失了,它不应该每分钟调用RunProgram(),直到我按stop?在上面的代码中,计时器设置为1000毫秒。您是否尝试在运行程序中放置调试器。控制装置在里面吗?你能把全部代码都贴出来吗?