Visual Studio 2005 Windows Service Starts but Timer does not execute unless in debug mode

This is a very interesting one. I am able to deploy my VS 2005 Windows Service with a release configuration but when I do so, it does not begin to execute the timer until I am attached to the process. The timer is enabled and set to execute every 3 seconds but never does. I am using an account in which the same service works in VS 2003 so I do not think it is permissions. If anyone has any insight into this behavior I would appreciate it thanks...

Answer this question

Visual Studio 2005 Windows Service Starts but Timer does not execute unless in debug mode

  • LalitSRana

    Ok I figured it out. The problem was with the timer I selected from the toolbox. The timer I selected from the toolbox was a windows forms timer. I pinpointed the problem to the fact that the timer was not executing, as previously stated, and compared it to the timer I used in my 2003 service. The timer that should be used by default is System.Timers.Timer!
  • Richard Proudfoot

    THANKS DMAN1

    Nothing major. I modified the AppConfigSettings code because it is different in 05 but everything else is the same which is what puzzles me. My 2003 service is still up and running without a problem. Another thing I noticed is the fact that I no longer have to attach to the MyService.exe file in order to debug. It is actually grayed out when I attempt to attach to it the same way I do in 2003. In order to debug it I discovered I could simply run the project after it had been started and it would attached. I could not find any documentation out there that discribed that behavior but I ignored it during debugging none the less. Now that I want to deploy it has become a bit of a problem...


  • mranzani

    DUring debug, set a break point in your timer event. Does the event ever get fired



  • Edmund

    Hi,

    I didn't want to start a new post as my problem is related to this. I am using the timer in my windows service. I try to enable it and then after an amount of time call a function. I have tried several intervals, but none seem to work. I am also using the System.timers. I am not really sure what I am doing wrong. Here is what I have:

    Public Class LicenseMonitorServ

    Dim StartApp As WindowsApplication2.Form1

    Protected Overrides Sub OnStart(ByVal args() As String)

    ' Add code here to start your service. This method should set things

    ' in motion so your service can do its work.

    Timer1.Enabled = True

    End Sub

    Protected Overrides Sub OnStop()

    ' Add code here to perform any tear-down necessary to stop your service.

    Timer1.Enabled = False

    End Sub

    Private Sub Timer1_Elapsed(ByVal sender As System.Object, ByVal e As System.Timers.ElapsedEventArgs) Handles Timer1.Elapsed

    StartApp.current = Now

    StartApp.Run()

    End Sub

    End Class

    In case it helps here is the designer code as well. Any help would be greatly appreciated.

    Imports System.ServiceProcess

    <Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _

    Partial Class LicenseMonitorServ

    Inherits System.ServiceProcess.ServiceBase

    'UserService overrides dispose to clean up the component list.

    <System.Diagnostics.DebuggerNonUserCode()> _

    Protected Overrides Sub Dispose(ByVal disposing As Boolean)

    If disposing AndAlso components IsNot Nothing Then

    components.Dispose()

    End If

    MyBase.Dispose(disposing)

    End Sub

    ' The main entry point for the process

    <MTAThread()> _

    <System.Diagnostics.DebuggerNonUserCode()> _

    Shared Sub Main()

    Dim ServicesToRun() As System.ServiceProcess.ServiceBase

    ' More than one NT Service may run within the same process. To add

    ' another service to this process, change the following line to

    ' create a second service object. For example,

    '

    ' ServicesToRun = New System.ServiceProcess.ServiceBase () {New Service1, New MySecondUserService}

    '

    ServicesToRun = New System.ServiceProcess.ServiceBase() {New LicenseMonitorServ}

    System.ServiceProcess.ServiceBase.Run(ServicesToRun)

    End Sub

    'Required by the Component Designer

    Private components As System.ComponentModel.IContainer

    ' NOTE: The following procedure is required by the Component Designer

    ' It can be modified using the Component Designer.

    ' Do not modify it using the code editor.

    <System.Diagnostics.DebuggerStepThrough()> _

    Private Sub InitializeComponent()

    Me.Timer1 = New System.Timers.Timer

    CType(Me.Timer1, System.ComponentModel.ISupportInitialize).BeginInit()

    '

    'Timer1

    '

    Me.Timer1.Interval = 60000

    '

    'MyService

    '

    Me.ServiceName = "MyService"

    CType(Me.Timer1, System.ComponentModel.ISupportInitialize).EndInit()

    End Sub

    Friend WithEvents Timer1 As System.Timers.Timer

    End Class

    OK it seems as if my error comes from the WindowsApplication2.Form function call. Is there any other way to go about doing this, because I want that function to be called when the timer elapses.


  • MichaelD&amp;#33;

    Hi All,

    I've a simple windows service written using VS 2005. This Windows Service with Timer works fine under XP and not VISTA. Please help

    #pragma once

    using namespace System;

    using namespace System::Collections;

    using namespace System:Tongue TiederviceProcess;

    using namespace System::ComponentModel;

    namespace safeagent {

    /// <summary>

    /// Summary for safeagentWinService

    /// </summary>

    ///

    /// WARNING: If you change the name of this class, you will need to change the

    /// 'Resource File Name' property for the managed resource compiler tool

    /// associated with all .resx files this class depends on. Otherwise,

    /// the designers will not be able to interact properly with localized

    /// resources associated with this form.

    public ref class safeagentWinService : public System:Tongue TiederviceProcess:Tongue TiederviceBase

    {

    public:

    safeagentWinService()

    {

    InitializeComponent();

    //

    //TODO: Add the constructor code here

    //

    OutputDebugString (L"Constructor");

    }

    protected:

    /// <summary>

    /// Clean up any resources being used.

    /// </summary>

    ~safeagentWinService()

    {

    if (components)

    {

    delete components;

    }

    }

    /// <summary>

    /// Set things in motion so your service can do its work.

    /// </summary>

    virtual void OnStart(array<String^>^ args) override

    {

    // TODO: Add code here to start your service.

    this->timer1->Enabled = true;

    }

    /// <summary>

    /// Stop this service.

    /// </summary>

    virtual void OnStop() override

    {

    // TODO: Add code here to perform any tear-down necessary to stop your service.

    this->timer1->Enabled = false;

    }

    private: System::Timers::Timer^ timer1;

    protected:

    protected:

    private: System::ComponentModel::IContainer^ components;

    private:

    /// <summary>

    /// Required designer variable.

    /// </summary>

    #pragma region Windows Form Designer generated code

    /// <summary>

    /// Required method for Designer support - do not modify

    /// the contents of this method with the code editor.

    /// </summary>

    void InitializeComponent(void)

    {

    this->components = (gcnew System::ComponentModel::Container());

    this->timer1 = (gcnew System::Timers::Timer());

    //

    // timer1

    //

    this->timer1->Enabled = true;

    this->timer1->Interval = 5000;

    this->timer1->Elapsed += gcnew System::Timers::ElapsedEventHandler(this, &safeagentWinService::timer1_Tick_1);

    //

    // safeagentWinService

    //

    this->CanPauseAndContinue = true;

    this->ServiceName = L"safeagentWinService";

    }

    #pragma endregion

    private: System::Void timer1_Tick_1(System:Surprisebject^ sender, System::Timers::ElapsedEventArgs^ e) {

    Beep (1000, 100);

    OutputDebugString (L"Called for every 5 sec");

    }

    };

    }


  • BradN

    What code WAS changed between 03 & 05

  • Ska Software

    Everything works fine in debug mode, I even deleted the original timer and created another just to ensure the code behind the timer was corrupted somehow. The timer fires and I am able to step through the code in debug mode. The timer isn't firing in release mode.
  • Visual Studio 2005 Windows Service Starts but Timer does not execute unless in debug mode