每5秒运行一次方法而不创建新的事件处理程序

时间:2015-02-06 14:06:56

标签: c# methods timer recurring

我需要在c#控制台程序中每5秒运行一个方法(该程序在一个单独的线程中打开一个Win表单UI)。

由于每次_timer.Elapsed(或_timer.Tick)达到0后事件处理程序没有被终止,我在创建计时器时的每次尝试都失败了。

这导致对我的方法的多次调用,随后我收集的信息被复制(每个事件处理程序3次)。

有没有人有一个我可以每隔5秒调用一次方法的例子,不会导致这种重复?

   public static System.Timers.Timer _timer = new System.Timers.Timer();


    static void Main(string[] args)
    {
        //Check Registry for existing Robo config
        RegistryTasks StartupRegistryCheck = new RegistryTasks();
        StartupRegistryCheck.StartupRegistryCheck();

        RetrieveJobs();

        _timer.Interval = 5000;
        _timer.Elapsed += OnTimedEvent;
        _timer.Enabled = true;

        new Thread(() =>
        {
            //Thread.Sleep(6000);
            load_UI();
        }).Start();
    }

    static void RetrieveJobs()
    {
        RegistryTasks getIPs = new RegistryTasks();
        getIPs.GetIPFromRegistry(Servers);
        //Console.WriteLine(Servers);
        foreach (object o in Servers)
        {
            dynamic expObj = new ExpandoObject();
            expObj = o;
            string IP = expObj.serverObjectIp;
            int Port = expObj.serverObjectPort;

            // Start synchronous socket connections
            string JobList = SocketClient.GetJobList(IP, Port);

            // Uncomment the lines below to use async sockets (currently not working)
            // IPHostEntry ipHostInfo = Dns.GetHostEntry(IP);
            // IPAddress ipAddress = ipHostInfo.AddressList[0];
            // IPEndPoint ipe = new IPEndPoint(ipAddress, Port);
            // Socket s = new Socket(AddressFamily.InterNetwork,
            // SocketType.Stream, ProtocolType.Tcp);
            // string JobList = ClientSocketAsync.Connect(ipe, s);

            // Split Retrieved JobLiST XML data into Arrays for later use.
            XmlSplitter.splitXml(JobList, out ActiveJobsArray, out CompletedJobsArray, out PendingJobsArray);
        }
    }

    public static void OnTimedEvent(object source, System.Timers.ElapsedEventArgs e)
    {
        _timer.Stop();
        RetrieveJobs();
        _timer.Start();
    }

有问题的代码如上:(

当前输出......在下面。

Timer hit 0.
Received data 1
Received data 2
Received data 3
Timer hit 0.
Received data 1
Received data 2
Received data 3
Received data 1
Received data 2
Received data 3
Timer hit 0.
Received data 1
Received data 2
Received data 3
Received data 1
Received data 2
Received data 3
Received data 1
Received data 2
Received data 3

更新......所以我仍然没有弄清楚为什么我的计时器不能复制而不重复自己。 Main只调用一次。在任何其他类中没有其他对计时器的引用。在前面提到的代码中可以找到唯一的计时器参考或提示。

因此,作为一项实验,看看它是否在新程序中得到了治愈,我创建了一个新的控制台应用程序。

我添加了一个计时器和一个“MessageBox.Show()”来阻止程序并阻止退出。可以在下面找到为此测试编写的100%代码,奇怪的是,程序立即退出,并且不显示任何MessageBox。计时器甚至不执行一个循环(因为AutoReset设置为false时会出现这种情况)。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ConsoleApplication1
{
    class Program
    {
        public static System.Timers.Timer _timer = new System.Timers.Timer();

    static void Main(string[] args)
    {
        _timer.AutoReset = false;
        _timer.Interval = 5000;
        _timer.Elapsed += OnTimedEvent;
        _timer.Enabled = true;
        _timer.Start();
    }

    static void OnTimedEvent(object source, System.Timers.ElapsedEventArgs e)
    {
        _timer.Stop();
        Console.WriteLine("Timer Stopped");
        //_timer.Elapsed -= OnTimedEvent;
        RetrieveJobs();
        _timer.Start();
        Console.WriteLine("Timer Started");
    }

    static void RetrieveJobs()
    {
        Console.WriteLine("Timer hit 0");
        MessageBox.Show("Blocking Timer Program Exit for Timer to run!");
    }
    }
}

现在已经解决了。请关闭。

在RetrieveJobs()foreach循环中,我的Servers对象变得越来越复杂。它不是列出3个服务器,而是从注册表中收集相同的3个服务器并将它们添加到对象中。我没有错,因为没有早点发现这一点。感谢所有提供帮助的人:)。

0 个答案:

没有答案