简体   繁体   English

System.threading.timer无法在Windows服务中运行

[英]System.threading.timer not working in Windows Service

I am using System.threading.timer in Windows Service. 我在Windows服务中使用System.threading.timer。 But the timer is not successfully executed.Below is the code. 但是计时器没有成功执行。下面是代码。

    protected override void OnStart(string[] args)
    {
        try
        {  
        eventLog1.WriteEntry("In OnStart");
        TimeSpan dueMinutes = TimeSpan.FromMinutes(1);
        TimeSpan fromMinutes = TimeSpan.FromMinutes(1);
        System.Threading.Timer timer = new System.Threading.Timer(new TimerCallback(CallBack), null, dueMinutes, fromMinutes);


            /*
        System.Timers.Timer timer = new System.Timers.Timer(5 * 60 * 1000);       
        timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);               
            DBSyncHandler sync = new DBSyncHandler();
            sync.startSync();                 
        */
        }
        catch (Exception ex)
        {
            if (!System.Diagnostics.EventLog.SourceExists("MySource"))
            {
                System.Diagnostics.EventLog.CreateEventSource("MySource", "MyEventLog");
            }
            eventLog1.Source = "MySource";
            eventLog1.Log = "MyEventLog";
            eventLog1.WriteEntry("Error : " + ex.Message);
        }

    }



    public static void CallBack(object sender)
    {

        try
        {
            DBSyncHandler sync = new DBSyncHandler();
            sync.startSync();
        }
        catch (Exception ex)
        {
            EventLog eventLog1 = new EventLog();
            if (!System.Diagnostics.EventLog.SourceExists("MySource"))
            {
                System.Diagnostics.EventLog.CreateEventSource("MySource", "MyEventLog");
            }
            eventLog1.Source = "MySource";
            eventLog1.Log = "MyEventLog";
            eventLog1.WriteEntry("Error : " + ex.Message);
        }

    }

After successfull installation .My workstation is restarted.On restarting the machine ,the service is called successfully.But once the service is called first time ,it is not repeating for next time duration ie the service is not called again. 安装成功后。我的工作站重新启动。重启机器后,服务成功调用。但是一旦第一次调用该服务,它就不会重复下一次持续时间,即不再调用该服务。

Read the notes on MSDN: http://msdn.microsoft.com/en-us/library/system.threading.timer.aspx 阅读MSDN上的说明: http//msdn.microsoft.com/en-us/library/system.threading.timer.aspx

As long as you are using a Timer, you must keep a reference to it. 只要您使用Timer,就必须保留对它的引用。 As with any managed object, a Timer is subject to garbage collection when there are no references to it. 与任何托管对象一样,当没有对它的引用时,Timer会进行垃圾回收。 The fact that a Timer is still active does not prevent it from being collected. Timer仍处于活动状态这一事实并不能阻止它被收集。

System.Threading.Timer is a simple, lightweight timer that uses callback methods and is served by thread pool threads. System.Threading.Timer是一个简单,轻量级的计时器,它使用回调方法并由线程池线程提供服务。 It is not recommended for use with Windows Forms, because its callbacks do not occur on the user interface thread. 不建议将其与Windows窗体一起使用,因为它的回调不会发生在用户界面线程上。 System.Windows.Forms.Timer is a better choice for use with Windows Forms. System.Windows.Forms.Timer是与Windows窗体一起使用的更好选择。 For server-based timer functionality, you might consider using System.Timers.Timer, which raises events and has additional features. 对于基于服务器的计时器功能,您可以考虑使用System.Timers.Timer,它会引发事件并具有其他功能。

I think that your timer object created in the OnStart is gc collected or disposed. 我认为在OnStart中创建的计时器对象是gc收集或处理的。 it should not be a local variable in that method as it runs out of scope. 它不应该是该方法中的局部变量,因为它超出了范围。

Your timer variable needs to be at the class level. 您的计时器变量需要在类级别。 Once it goes out of scope, it won't run anymore. 一旦它超出范围,它将不再运行。

Microsoft's recommendation is to use System.Timers.Timer in server code. Microsoft建议在服务器代码中使用System.Timers.Timer。

Further information is available on the MSDN web site. 有关详细信息,请访问MSDN网站。 http://msdn.microsoft.com/en-us/library/system.threading.timer.aspx http://msdn.microsoft.com/en-us/library/system.threading.timer.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM