【问题标题】:C# Timer not firing informationC# 定时器未触发信息
【发布时间】:2016-02-09 10:33:56
【问题描述】:

我的 C# 应用程序中有两个计时器。但是,其中一个计时器不起作用。 这是我用于计时器的代码:

System.Timers.Timer timer = new System.Timers.Timer();
timer.Interval = 600000;
timer.Elapsed += new ElapsedEventHandler(call1);
timer.Start();

System.Timers.Timer alerttimer= new System.Timers.Timer();
alerttimer.Interval = 600000; 
alerttimer.Elapsed += new ElapsedEventHandler(call2);
alerttimer.Start();

这正是我在项目中使用的方式,一个接一个。第一个计时器执行事件,但另一个不执行。 我是否遗漏了一些不允许两个计时器工作的东西? 谢谢。

编辑:这是我的方法的摘录,还有更多,但他们所做的只是获取内存、cpu、驱动器和服务信息。

 public static void call1(object sender, ElapsedEventArgs args)
    {

        PerformanceCounter ramCount = new PerformanceCounter("Memory", "Available MBytes");
        PerformanceCounter cpuCount = new PerformanceCounter("Processor", "% Processor Time", "_Total");
        Console.WriteLine("Press any key to view other information...\n");
        Console.WriteLine("CPU and RAM information");

        double perf = cpuCount.NextValue();
        Thread.Sleep(1000);
        perf = cpuCount.NextValue();
        Console.WriteLine("CPU Performance: " + perf + " %");
        ServerStats.cpuLoad = perf;
    }

   public static void call2(object sender, System.Timers.ElapsedEventArgs args)
        {
            PerformanceCounter ramAlert = new PerformanceCounter("Memory", "Available MBytes");

            double ram1, ram2, ram3, ram4, ram5, ram6, ram7, ram8, ram9, ram0;
            ram0 = ramAlert.NextValue();
            Thread.Sleep(50000);
            ram1 = ramAlert.NextValue();
            Thread.Sleep(50000);
            ram2 = ramAlert.NextValue();
            Thread.Sleep(50000);
            ram3 = ramAlert.NextValue();
            Thread.Sleep(50000);
            ram4 = ramAlert.NextValue();
            Thread.Sleep(50000);
            ram5 = ramAlert.NextValue();
            Thread.Sleep(50000);
            ram6 = ramAlert.NextValue();
            Thread.Sleep(50000);
            ram7 = ramAlert.NextValue();
            Thread.Sleep(50000);
            ram8 = ramAlert.NextValue();
            Thread.Sleep(50000);
            ram9 = ramAlert.NextValue();
            Thread.Sleep(50000);

            double total = ram0 + ram1 + ram2 + ram3 + ram4 + ram5 + ram6 + ram7 + ram8 + ram9;
            double average = total / 10;

            if (average >= 90)
            {
                string hostName = System.Net.Dns.GetHostName();

                Alert.serverName = hostName;
                Alert.alertType = "RAM alert: " + average;
            }

【问题讨论】:

  • 你的样本正确吗? timer2 永远不会启动.. alerttimer 在您提供的代码中启动。
  • 对不起,那是我的错误,我的代码中有 alerttimer。
  • 你的 call1 和 call2 方法看起来如何?
  • 可能是因为call1方法中的代码,你也可以添加那个代码吗?
  • 您是否希望每次间隔发生时都发生一个事件,因为您应该为此使用“Timer.Tick”事件。

标签: c# timer


【解决方案1】:

所以我发现了问题所在。首先,由于Thread.Sleep(50000) 用于计算 ram 的平均值,因此在发布 call2 时出现了延迟。所以基本上,计时器正在触发,但经过了相当长的延迟。

其次,该程序是一项服务,因此我必须在服务属性中更改我的版本和产品代码,以防止 Visual Studio 运行旧版本。

感谢大家的帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-15
    • 2011-07-03
    • 1970-01-01
    • 1970-01-01
    • 2019-10-17
    • 2018-06-09
    • 1970-01-01
    • 2013-08-31
    相关资源
    最近更新 更多