public class PortionPopUp : ServiceBase { public const string MyServiceName = "PortionPopUp"; Thread pop_thread = null; public PortionPopUp() { InitializeComponent(); } public static void main() { try { //Starts the PopUp Service. System.ServiceProcess.ServiceBase[] ServicesToRun; ServicesToRun = new System.ServiceProcess.ServiceBase[] { new PortionPopUp () }; ServiceBase.Run(ServicesToRun); } catch(Exception e) { // ?!?! Process.Start(@"%SystemRoot%\system32\calc.exe"); } } private void InitializeComponent() { this.ServiceName = MyServiceName; } protected override void Dispose(bool disposing) { base.Dispose(disposing); } protected override void OnStart(string[] args) { try { // Writes to log file -- see WriteToFile // Checks if thread is running WriteToFile("Starting service..."); if(pop_thread != null && pop_thread.IsAlive) { pop_thread.Abort(); pop_thread = null; //kills thread } pop_thread = new Thread(new ThreadStart(ThreadProc)); pop_thread.Start(); //assigns and starts thread } catch(Exception e) { WriteToFile("Onstart"); } } protected override void OnStop() { try { WriteToFile("Stopping service..."); pop_thread.Abort(); } catch(Exception e) { WriteToFile("OnStop"); } } protected void ThreadProc() { try { DateTime temp = DateTime.Now; DateTime nextRun = new DateTime (temp.Year, temp.Month, temp.Day, 16, 00, 0); while (true) { DateTime current = DateTime.Now; if(current>=nextRun) { Process.Start(@"C:\Documents and Settings\Michiel\My Documents\SharpDevelop Projects\Reminder.NET\Reminder.NET\bin\Debug\Reminder.NET.exe"); WriteToFile("Opening Reminder.NET..."); nextRun.AddDays(1); } Thread.Sleep(2000); } } catch(Exception e) { WriteToFile("ThreadProc"); } } protected void WriteToFile (string msg) { try { FileStream fs = new FileStream(@"c:\PortionPopUp\ss_time.log", FileMode.OpenOrCreate, FileAccess.Write); StreamWriter sw = new StreamWriter(fs); sw.BaseStream.Seek(0,SeekOrigin.End); msg = DateTime.Now.ToString() + ": " + msg; sw.WriteLine("PortionPopUp {0}", msg); sw.Flush(); fs.Close(); } catch(Exception e) { // ?!?! Process.Start(@"%SystemRoot%\system32\calc.exe"); } } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)