1

I have a created a windows service and created installer for it. It is installed and started but the code i have written inside it is not executing. actually when i start the service from the service window the OnStart() function is not fired. nor the initializecomponent() nor the static void main function.. can any one help me with it

please guide me where i have done wrong.

here are some code lines. let me know if u want more of what i have written

public partial class iMArchiveService : ServiceBase
{
    Boolean isArchiving = false;

    public iMArchiveService()
    {
        MyException.CreateLog("iMArchiveService: Inside Constructor. Initializing Component");
        InitializeComponent();
        MyException.CreateLog("iMArchiveService: Component Initialized. Timer is set as: " + TimeMachine.Interval.ToString() + " milliseconds");
    }

    protected void TimeMachine_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        try
        {
            MyException.CreateLog("iMArchiveService: Inside Tick Try. Value of isArchiving variable before condition is: " + isArchiving.ToString());
            if (!isArchiving)
                isArchiving = new iM.OrderArchiving.ArchiveOrderXML().ArchiveOrderService();
            MyException.CreateLog("iMArchiveService: Inside Tick Try. Value of isArchiving variable after condition is: " + isArchiving.ToString());
        }
        catch (Exception ex)
        {
            MyException.CreateLog("iMArchiveService: Inside Tick Catch :(");
        }
    }

    protected override void OnStart(string[] args)
    {
        TimeMachine.Enabled = true;
        MyException.CreateLog("iMArchiveService Started: " + DateTime.Now.ToString());
    }

    protected override void OnStop()
    {
        TimeMachine.Enabled = false;
        MyException.CreateLog("iMArchiveService Stopped: " + DateTime.Now.ToString());
    }

}

the code above is for service file.cs

here is my project installer file

namespace iM.OrderArchivingService
{
    [RunInstaller(true)]
    public partial class ProjectInstaller : Installer
    {
        public ProjectInstaller()
        {
        InitializeComponent();
        }
    }
}

here is the InitializeComponenet Function -

private void InitializeComponent()
    {
        this.myServiceProcessInstaller = new System.ServiceProcess.ServiceProcessInstaller();
        this.myServiceInstaller = new System.ServiceProcess.ServiceInstaller();
        // 
        // myServiceProcessInstaller
        // 
        this.myServiceProcessInstaller.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
        this.myServiceProcessInstaller.Installers.AddRange(new System.Configuration.Install.Installer[] {
        this.myServiceInstaller});
        this.myServiceProcessInstaller.Password = null;
        this.myServiceProcessInstaller.Username = null;
        // 
        // myServiceInstaller
        // 
        this.myServiceInstaller.ServiceName = "iMArchiveService";
        // 
        // ProjectInstaller
        // 
        this.Installers.AddRange(new System.Configuration.Install.Installer[] {
        this.myServiceProcessInstaller});

    }

here is the program.cs file

namespace iM.OrderArchivingService
{
static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    static void Main(string[] args)
    {
        ServiceBase[] ServicesToRun;
        ServicesToRun = new ServiceBase[] { new iMArchiveService() };
        ServiceBase.Run(ServicesToRun);
    }
}
}

as u see i have written the code to log when in initializes or starts.. but no log is being made.

edit:

Code for Timer(TimeMachine)

    private void InitializeComponent()
    {
        this.components = new System.ComponentModel.Container();
        this.TimeMachine = new System.Timers.Timer(3600000);
        // 
        // TimeMachine
        // 
        this.TimeMachine.Interval = 3600000;
        this.TimeMachine.Elapsed += new System.Timers.ElapsedEventHandler(TimeMachine_Elapsed);
        // 
        // iMArchiveService
        // 
        this.ServiceName = "iMArchiveService";

    }

thnx

1Mayur
  • 3,269
  • 5
  • 36
  • 63
  • What is the interval of TimeMachine timer ? – Coder Apr 20 '12 at 06:23
  • added the timer code.. but atleast it should write the log which i have coded on start. the log is also not written for that – 1Mayur Apr 20 '12 at 07:05
  • I guess its not the timer issue... logging further shows that it reaches the projectinstaller initialize componenet function but it is not getting what has to be started. i mean my initialize function need to have a reference to my servicebase class... – 1Mayur Apr 20 '12 at 09:35

4 Answers4

1

You're using the wrong Timer class - the clue is in its namespace: System.Windows.Forms.Timer. That timer only works in WinForms applications.

You should, instead, switch to using System.Timers.Timer


There is a general discussion of the timer classes in System.Threading.Timer:

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.

(My emphasis replaces original)

Damien_The_Unbeliever
  • 220,246
  • 21
  • 302
  • 402
1

Don't use System.Windows.Forms.Timer in windows service, it might not raise events in it. See The Windows Forms Timer event is not raised in a Windows service.

Use either System.Timers.Timer or System.Threading.Timer in windows service. See Windows service and timer.

Community
  • 1
  • 1
Coder
  • 3,782
  • 1
  • 22
  • 35
  • its not the timer issue.. coz for that it has to reach to service. but still this would have caused problems later so fixed it, Thanks – 1Mayur Apr 20 '12 at 09:34
0

Please check TimeMachine.Enable = True and have you set timing for timer or not.

refer this link

http://codesimplified.com/2010/12/31/creating-window-service-with-net-vs2008/

Viral Sarvaiya
  • 771
  • 2
  • 9
  • 29
0

Maybe you should use the Timer.Start() and Timer.Stop() methods to start and stop the timer instead, just in case there is a problem using the Enabled property.

The interval period is 3,600,000 which is 3600 seconds or 60 minutes = 1 hour. Nothing will happen until an hour has passed by; this is what you intended?

BTW, setting the interval like the examples below will make your code a little easier to read:

this.TimeMachine.Interval = 1 * 1000;  // 1 Second
this.TimeMachine.Interval = 60 * 1000; // 60 Seconds
this.TimeMachine.Interval = 60 * 60 * 1000; // 1 hour

Try using the Debug.Writeline() method in System.Diagnostics. By default that will post messages in the Output window in MSVS. You'll also see any exceptions there too.