Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>From <a href="http://msdn.microsoft.com/en-us/library/system.serviceprocess.servicebase.onstart(VS.85).aspx" rel="noreferrer">MSDN</a>:<br> "Do not use the constructor to perform processing that should be in OnStart. Use OnStart to handle all initialization of your service. The constructor is called when the application's executable runs, not when the service runs. The executable runs before OnStart. When you continue, for example, the constructor is not called again because the SCM already holds the object in memory. If OnStop releases resources allocated in the constructor rather than in OnStart, the needed resources would not be created again the second time the service is called."<br><br> If your timer is not initialized in the OnStart call, this could be a problem. I would also check the type of timer, make sure its a System.Timers.Timer for Services. <a href="http://clifgriffin.com/2008/11/20/using-timers-in-a-c-windows-service/" rel="noreferrer">Here</a> is an example of how to setup the timer in a windows service.</p> <p><a href="http://weblogs.asp.net/jgalloway/archive/2005/10/24/428303.aspx" rel="noreferrer">//TODONT: Use a Windows Service just to run a scheduled process</a></p> <p>I tried your code, and it seems ok. The only difference I had was to hard code the timer value (Service1.cs). Let me know if the below doesnt work.</p> <p>Service1.cs</p> <pre><code>using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.ServiceProcess; using System.Text; using System.Timers; using System.Threading; namespace WindowsServiceTest { public partial class Service1 : ServiceBase { private System.Timers.Timer timer; public Service1() { InitializeComponent(); } protected override void OnStart(string[] args) { //instantiate timer Thread t = new Thread(new ThreadStart(this.InitTimer)); t.Start(); } protected override void OnStop() { timer.Enabled = false; } private void InitTimer() { timer = new System.Timers.Timer(); //wire up the timer event timer.Elapsed += new ElapsedEventHandler(timer_Elapsed); //set timer interval //var timeInSeconds = Convert.ToInt32(ConfigurationManager.AppSettings["TimerIntervalInSeconds"]); double timeInSeconds = 3.0; timer.Interval = (timeInSeconds * 1000); // timer.Interval is in milliseconds, so times above by 1000 timer.Enabled = true; } protected void timer_Elapsed(object sender, ElapsedEventArgs e) { int timer_fired = 0; } } } </code></pre> <p>Service1.Designer.cs</p> <pre><code>namespace WindowsServiceTest { partial class Service1 { /// &lt;summary&gt; /// Required designer variable. /// &lt;/summary&gt; private System.ComponentModel.IContainer components = null; /// &lt;summary&gt; /// Clean up any resources being used. /// &lt;/summary&gt; /// &lt;param name="disposing"&gt;true if managed resources should be disposed; otherwise, false.&lt;/param&gt; protected override void Dispose(bool disposing) { if (disposing &amp;&amp; (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Component Designer generated code /// &lt;summary&gt; /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// &lt;/summary&gt; private void InitializeComponent() { components = new System.ComponentModel.Container(); this.ServiceName = "Service1"; this.CanPauseAndContinue = true; } #endregion } } </code></pre> <p>I just created a blank Windows Service project and add the below so I could run installutil.exe and attach to the above to see if the event was firing (and it did).</p> <pre><code>using System; using System.Collections.Generic; using System.Text; using System.ComponentModel; using System.ServiceProcess; namespace WindowsServiceTest { [RunInstaller(true)] public class MyServiceInstaller : System.Configuration.Install.Installer { public MyServiceInstaller() { ServiceProcessInstaller process = new ServiceProcessInstaller(); process.Account = ServiceAccount.LocalSystem; ServiceInstaller serviceAdmin = new ServiceInstaller(); serviceAdmin.StartType = ServiceStartMode.Manual; serviceAdmin.ServiceName = "Service1"; serviceAdmin.DisplayName = "Service1 Display Name"; Installers.Add(process); Installers.Add(serviceAdmin); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload