Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.threading.timer not working in Windows Service

I am using System.threading.timer in Windows Service. 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 i.e. the service is not called again.

like image 609
sachin kulkarni Avatar asked Dec 06 '22 19:12

sachin kulkarni


2 Answers

Read the notes on 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. As with any managed object, a Timer is subject to garbage collection when there are no references to it. The fact that a Timer is still active does not prevent it from being collected.

System.Threading.Timer is a simple, lightweight timer that uses callback methods and is served by thread pool threads. It is not recommended for use with Windows Forms, because its callbacks do not occur on the user interface thread. System.Windows.Forms.Timer is a better choice for use with Windows Forms. For server-based timer functionality, you might consider using System.Timers.Timer, which raises events and has additional features.

I think that your timer object created in the OnStart is gc collected or disposed. it should not be a local variable in that method as it runs out of scope.

like image 73
Davide Piras Avatar answered Jan 05 '23 01:01

Davide Piras


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.

Further information is available on the MSDN web site. http://msdn.microsoft.com/en-us/library/system.threading.timer.aspx

like image 45
Brad Bruce Avatar answered Jan 04 '23 23:01

Brad Bruce