Windows Services - Is there any code that cannot run in a service?

At startup, I need to check the status of a variable on my systems. First I tried using task scheduler to launch the code at boot, but it didn't work, so I am now looking into just writing a windows service. The idea is, I launch a dos based utility to check the stauts of the variable (I cannot check it directly it is proprietary), storing the status in a registry key. After the utility completes, I read the key. If the status is correct, I want to check the time. If the time is within a certain timeframe, it should launch a seperate program. The service seems to start ok, but as soon as it goes to check the time, it stops working.

Code Below:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using Impersonationator;
using System.Management;
using Microsoft.Win32;


namespace CheckThawState
{
public partial class CheckThawState : ServiceBase
{
public CheckThawState()
{
InitializeComponent();
}

protected override void OnStart(string[] args)
{
SetfrzStateKey();//checks status, sets key
RegistryKey myKey = Registry.LocalMachine.OpenSubKey("Software\\Educational Technology"
+ "\\LaunchPad", true);
String state = myKey.GetValue("frzstate").ToString();
#region Thawed/Frozen Check
if (state == "thawed")
{
Boolean TimeCheck = ChkTime();
if (TimeCheck == true)
{
Console.Beep();//This never happens. The code never gets here.
StartLaunchpad();
}
else
{
return;
}
}
else if (state == "frozen")
{
return;
}
else return;
#endregion
}//end onStart method

//=============================================================

static bool ChkTime()
{
DateTime Now = DateTime.Now;
DateTime Today = DateTime.Today;
//if current time is after 3, but before 4, run updates
if (DateTime.Compare(Now, Today.AddHours(3)) > 0 && DateTime.Compare(Now, Today.AddHours(4)) < 0)
{
return true;
}
else return false;
}//end chktime

//=======================================================

static void StartLaunchpad()
{
using (new Impersonator("Administrator", Environment.MachineName, "raster2919"))
{

ManagementClass processClass = new ManagementClass("Win32_Process");
object[] methodArgs = { "c:\\slu_refresh\\lpupdater.exe /info", "c:\\slu_refresh\\", null, 0 };
object result = processClass.InvokeMethod("Create", methodArgs);
}
}//end startlaunchpad method

//==========================================================

static void SetfrzStateKey()
{
using (new Impersonator("Administrator", Environment.MachineName, "raster2919"))
{
ManagementClass processClass = new ManagementClass("Win32_Process");
object[] methodArgs = { "c:\\slu_refresh\\dfc.bat", "c:\\slu_refresh\\", null, 0 };
object result = processClass.InvokeMethod("Create", methodArgs);
ManagementObjectSearcher myQuery = new ManagementObjectSearcher("SELECT * FROM Win32_Process WHERE ProcessID = "
+ methodArgs[3]);
ManagementObjectCollection myCollection = myQuery.Get();
while (myCollection.Count > 0)
{
//While the batch file is running, wait
myCollection = myQuery.Get();
}
}//end Impersonation
}//end setrzstatekey

//==============================================================

protected override void OnStop()
{
// TODO: Add code here to perform any tear-down necessary to stop your service.
}
protected override void OnContinue()
{
base.OnContinue();
Stop();
}
protected override void OnCustomCommand(int command)
{
base.OnCustomCommand(command);
Stop();
}
}//end partial class
}//end namespace




Answer this question

Windows Services - Is there any code that cannot run in a service?

  • Shri Borde

    Hi!

    Are you sure your ChkTime() is working Create console application and run it there. Also you can compare things easier - Now.Hour == 3.

    BTW, Windows service have limited time to start and perform it's activity. If I remember right, 30 seconds to start service. If not started - Windows terminate service.


  • Martin Sawicki

    Yes, when I run ChkTime() as a console app it works fine. That's actually the reason the subject is titled the way it is. It seems like chkTime() just doesn't run in the service, which sounds absurd at best.

    If Windows terminates the service, does it also kill processes spawned by that service If I need to run code that will take longer than 30 seconds, but I need (requirment cannot be changed) it to run at boot (ie before anyone logs in) is there a better way to do this


  • Janne W

    I don't think OnCustomCommand() have such restrictions, but I think it's good to read here http://msdn.microsoft.com/library/en-us/dllproc/base/service_entry_point.asp


  • ETJorg

    I think process may continue work, services have time limits, but this is easily overriden by creating new thread. Can you add tracing to your service, so you can find point where it's executed and where it doesn't


  • hinshelm78

    Because of what you said in your earlier post about the OnStart method needing to be short, I modified the code so that the bulk of the application is run using the OnCustomCommand and a timer. When I run the service now, ChkTime() runs without any problems. I have a feeling, what ChkTime is calling was taking to long, and so Windows was terminating the service.

    Do you know if there are any time constraints for the OnCustomCommand The application that will run could take anywhere from 30 seconds to 1 hour to execute depending on the system.


  • Windows Services - Is there any code that cannot run in a service?