I need to check the state of a computer at boot time, and depending on that
state, launch an application before a user logs in. When the service starts,
it runs a batch file, which gets the state of the computer, and stores the
information in a registry key, then the service reads the key to determine
the state. If the state meets the correct parameters, the service checks to
see what the time is. The application can only run during a set amount of
time. If the time is correct (between 3 and 4 am) the application should
launch. The problem is, it doesn't seem to get to the point where it has
checked the time. It passes through the status check just fine, but never
returns from the time check. In fact, it never enters the time check. I
placed a console.beep in there so I could monitor the progress pre-login, and
it never beeps. Putting beeps in strategic locations up to this point come
out fine.
Here is the code for the OnStart method:
protected override void OnStart(string[] args)
{
SetfrzStateKey();//sets the key below
RegistryKey myKey =
Registry.LocalMachine.OpenSubKey("Software\\Educat ional Technology"
+ "\\LaunchPad", true);
String state = myKey.GetValue("frzstate").ToString();
#region Thawed/Frozen Check
if (state == "thawed")
{
Boolean TimeCheck = ChkTime();
if (TimeCheck == true)
{
Console.Beep();
StartLaunchpad();
}
else
{
return;
}
}
else if (state == "frozen")
{
return;
}
else return;
#endregion
}
Here are the supporting functions:
static bool ChkTime()
{
Console.Beep();
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,
"myadminpassword"))
{
ManagementClass processClass = new ManagementClass("Win32_Process");
object[] methodArgs = { "c:\\slu_refresh\\lpupdater.exe /refresh",
"c:\\slu_refresh\\", null, 0 };
object result = processClass.InvokeMethod("Create", methodArgs);
}
}//end startlaunchpad method
//==================================================
static void SetfrzStateKey()
{
using (new Impersonator("Administrator", Environment.MachineName,
"myadminpassword"))
{
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
}
}//end Impersonation
}//end setrzstatekey