Service problems

  • Thread starter Thread starter cody
  • Start date Start date
C

cody

I can't seem to get this windows service to work properly, I get a
message about some services stopping automatically if they have no work
to do, I suspect it may be with the way I start the timer in the
OnStart method, any help is greatly appreciated.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Runtime.InteropServices;
using System.Timers;

namespace XpedX10
{
public partial class Service1 : ServiceBase
{
[DllImport("winmm.dll")]
private static extern bool PlaySound(string lpszName, int
hModule, int dwFlags);
X10Unified.Senders.Firecracker f;
Service.gLink serv = new Service.gLink();
String some;
Timer time = new Timer();
int orders = 0;
int oldorders = 0;

public Service1()
{
InitializeComponent();
}

protected override void OnStart(string[] args)
{
time.Interval = 30000;
time.Elapsed += new ElapsedEventHandler(time_Elapsed);
time.Enabled = true;
time.Start();
f =
X10Unified.Senders.Firecracker.GetInstance(Int32.Parse(System.Configuration.ConfigurationSettings.AppSettings["COM_Port"]));
}

void time_Elapsed(object sender, ElapsedEventArgs e)
{
try
{
orders = serv.newXpedx();
if (orders > 0 && oldorders == 0)
{
f.SendCommand('G', 1,
X10Unified.Senders.Firecracker.Commands.TurnOn);
PlaySound("c:\\windows\\media\\tada.wav", 0, 1);
}
if (orders == 0 && oldorders != 0)
{
f.SendCommand('G', 1,
X10Unified.Senders.Firecracker.Commands.TurnOff);
}
oldorders = orders;
}
catch (Exception ex)
{
}
}

protected override void OnStop()
{
// TODO: Add code here to perform any tear-down necessary
to stop your service.
}
}
}
 
So, I moved some of the timer code out of the OnStart, thinking that it
might not work if it was thinking it was going forever, but it still
doesn't work...it now looks like this

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Runtime.InteropServices;
using System.Timers;

namespace XpedX10
{
public partial class Service1 : ServiceBase
{
[DllImport("winmm.dll")]
private static extern bool PlaySound(string lpszName, int
hModule, int dwFlags);
X10Unified.Senders.Firecracker f;
Service.gLink serv = new Service.gLink();
Timer time = new Timer();
int orders = 0;
int oldorders = 0;

public Service1()
{
InitializeComponent();
time.Interval = 30000;
time.Elapsed += new ElapsedEventHandler(time_Elapsed);
f =
X10Unified.Senders.Firecracker.GetInstance(Int32.Parse(System.Configuration.ConfigurationSettings.AppSettings["COM_Port"]));
}

protected override void OnStart(string[] args)
{
time.Enabled = true;
}

void time_Elapsed(object sender, ElapsedEventArgs e)
{
try
{
orders = serv.newXpedx();
if (orders > 0 && oldorders == 0)
{
f.SendCommand('G', 1,
X10Unified.Senders.Firecracker.Commands.TurnOn);
PlaySound("c:\\windows\\media\\tada.wav", 0, 1);
}
if (orders == 0 && oldorders != 0)
{
f.SendCommand('G', 1,
X10Unified.Senders.Firecracker.Commands.TurnOff);
}
oldorders = orders;
}
catch (Exception ex)
{
}
}

protected override void OnStop()
{
// TODO: Add code here to perform any tear-down necessary
to stop your service.
}
}
}
 
Back
Top