windows服务c#vs2013中未触发计时器已用事件

windows服务c#vs2013中未触发计时器已用事件,c#,C#,我的windows服务中没有触发计时器已过事件,为什么?我在论坛中搜索,但没有任何解决方案适合我 在program.cs的主要内容中: static class program { static void Main() { #if DEBUG servicioCR cr = new servicioCR(); cr.beginProcess(); #else #endif

我的windows服务中没有触发计时器已过事件,为什么?我在论坛中搜索,但没有任何解决方案适合我

在program.cs的主要内容中:

static class program
{
    static void Main()
    {
        #if DEBUG
            servicioCR cr = new servicioCR();
            cr.beginProcess();
        #else
        #endif


        //ServiceBase[] ServicesToRun;
        //ServicesToRun = new ServiceBase[] 
        //{ 
        //    new servicioCR() 
        //};
        //ServiceBase.Run(ServicesToRun);
    }

}
在我的服务课上

    public static System.Timers.Timer timer;

    public servicioCR()
    {
        timer = new System.Timers.Timer(5000);
        timer.AutoReset = false;

        timer.Elapsed += timer_Elapsed;

        InitializeComponent();
    }
经过的事件

    private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        try
        {
            timer.Stop();

            //Do stuff
        }
        catch (System.Exception ex)
        {
            EventLog.WriteEntry(ex.Message);
        }
        finally
        {
            timer.Start();
        }
    }
和beginProcess()

我正在使用.NET Framework 4.5和VS 2013。。。我不明白为什么它不起作用,我从另一个工作正常的解决方案中复制了这个

如果我在经过的事件上的Do stuff的一行中放置一个断点,它永远不会中断


为什么??谢谢

您的计时器正在运行,但程序会在计时器启动之前自动关闭。你需要暂停一些时间,这样你的程序就不会自动关闭

static void Main()
{
    #if DEBUG
        servicioCR cr = new servicioCR();
        cr.beginProcess();
        Console.WriteLine("Program Running");
        Console.ReadLine();
    #else
    #endif


    //ServiceBase[] ServicesToRun;
    //ServicesToRun = new ServiceBase[] 
    //{ 
    //    new servicioCR() 
    //};
    //ServiceBase.Run(ServicesToRun);
}
这是我以前的一个项目中的一小段代码,它可以让你将程序作为服务和控制台应用程序运行。在Visual studio中,只需将调试器设置为在设置项目屏幕中传入参数
--console

class Program
{
    public static void Main(string[] args)
    {
        var example = new MyExampleApp();
        if (args.Contains("--console"))
        {
            example.ConsoleRun(args);
        }
        else
        {
            ServiceBase.Run(example);
        }
    }
}

class MyExampleApp : ServiceBase
{

    public void ConsoleRun(string[] args)
    {
        Console.WriteLine(string.Format("{0}::starting...", GetType().FullName));

        OnStart(args);

        Console.WriteLine(string.Format("{0}::ready (ENTER to exit)", GetType().FullName));
        Console.ReadLine();

        OnStop();

        Console.WriteLine(string.Format("{0}::stopped", GetType().FullName));
    }

    //... the rest of the code from your service class.
}

请向我们展示所有
程序.cs
,您的程序可能在计时器启动前关闭。您向我们展示的所有内容都是正确的,因此您的问题在于您没有向我们展示的代码。如果没有更多信息,您的问题将无法回答。@ScottChamberlain我将更新问题。您还应该在InitializeComponent()之后创建计时器;C#中计时器的一个常见问题是,它们在有机会激活之前就被收集到垃圾。为了防止这种情况发生,必须告诉GC不要处理特定的代码位。我读到的所有关于StackOverflow的C#timer问题都有这个问题,需要相同的解决方案。你需要解释“不工作”的含义。它有什么行为?程序是否只是关闭,是否编译,是否启动但计时器从未启动?程序启动但计时器从未启动并关闭。
class Program
{
    public static void Main(string[] args)
    {
        var example = new MyExampleApp();
        if (args.Contains("--console"))
        {
            example.ConsoleRun(args);
        }
        else
        {
            ServiceBase.Run(example);
        }
    }
}

class MyExampleApp : ServiceBase
{

    public void ConsoleRun(string[] args)
    {
        Console.WriteLine(string.Format("{0}::starting...", GetType().FullName));

        OnStart(args);

        Console.WriteLine(string.Format("{0}::ready (ENTER to exit)", GetType().FullName));
        Console.ReadLine();

        OnStop();

        Console.WriteLine(string.Format("{0}::stopped", GetType().FullName));
    }

    //... the rest of the code from your service class.
}