Jump to content

Odd c# service issue (code works in 12 programs, but not this one)

DianeLeigh

Something must be out of place or something--as this is basically identical (shy of what the code does) as 12 other services--but this one doesn't work right.

static void Main()
{
            EventLog eventLog = new EventLog();
            eventLog.Source = "itssource";
            eventLog.Log = "itslog";
            try
            {
                ((ISupportInitialize)(eventLog)).BeginInit();
                if (!EventLog.SourceExists(eventLog.Source))
                {
                    EventLog.CreateEventSource(eventLog.Source, eventLog.Log);
                }
            }
            catch (Exception e) { }
// note: the above works fine
            try
            {
                logit.WriteLogFile("program starting", "Program.Main");
#if !DEBUG
                // Start the Actual service
                ServiceBase[] ServicesToRun;
                ServicesToRun = new ServiceBase[]
                {
                new OakShortTimed()
                };
                ServiceBase.Run(ServicesToRun);
#endif
                logit.WriteLogFile("program Ended", "Program.Main");
            }
            catch (Exception e)
            {
                logit.WriteLogFile("Error in startup: " + e.Message, "Program.Main");
            }
}
// I just removed the code that either 1) works, or 2) doesn't apply to the startup / variable declarations

        public OakShortTimed()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {// Branch off into a timed actual program start (so service starts instantly)
            MainExecution = new Thread(StartProgram);
            MainExecution.Start();
        }

        private void StartProgram()
        {// If we use the line below, it starts instantly
         //_timer = new Timer(MainExecutionCallback, null, 1000 * 20, Timeout.Infinite);
         // If using this line, it starts, waits 20 seconds "logs program started" a second time, and is started for real
            _timer_ManpowerProfileNotifier = new Timer(ManpowerProfileNotifierCallback, null, 1000*20, Timeout.Infinite);
        }

        private void MainExecutionCallback(Object State)
        {// If this is called above, it will start instantly
        // Can put other code here too--and it still starts instantly--including code from the other function
        	_timer = new Timer(MainExecutionCallback, null, 1000 * 20, Timeout.Infinite);
        }

        private void ManpowerProfileNotifierCallback(Object State)
        {
            logit.WriteLogFile("Entry of Manpower Notifier Callback", "OakShortTimed.MPNC");
            // First we need to get the acutal runtime callback for the timer
            int ExecuteSeconds = 0;
            int ExecuteMinutes = 5;

            ManpowerRuntimes(out ExecuteMinutes, out ExecuteSeconds);
            if (!ManpowerProfileNotifierRunning)
            {
                if (DateTime.Now.Minute.IsBetween(ExecuteMinutes - 5, ExecuteMinutes + 5))
                {
                    if (!ManpowerProfileNotifierRunning)
                    {
                        ManpowerProfileNotifierRunning = true; 
                        Thread Thread_ManpowerProfileNotifier = new Thread(ManpowerProfileNotify.CheckForManpowerProfileEdits);
                        Thread_ManpowerProfileNotifier.Start();
                    }
                }
            }// end of !ManpowerProfileNotifierRunning

            DateTime dateTime = DateTime.Now;
            int ms = (ExecuteSeconds * 1000) + (ExecuteMinutes * 60 * 1000);

            dateTime = dateTime.AddMilliseconds(ms);
            TimeSpan ts = dateTime - DateTime.Now;
            ms = (int)ts.TotalMilliseconds;
            logit.WriteLogFile("Next Manpower hit: " + dateTime.ToShortTimeString(), "OakShortTimed.MPNC");
            _timer_ManpowerProfileNotifier = new Timer(ManpowerProfileNotifierCallback, null, ms, Timeout.Infinite);
        }









        private void ManpowerRuntimes(out int Minutes, out int Seconds)
        {
            // ManpowerProfileNotifier
            int ExecuteSeconds = 0;
            int ExecuteMinutes = 5;
            try
            {
                int.TryParse(ConfigurationManager.AppSettings["execute_every_seconds"].ToString(), out ExecuteSeconds);
                int.TryParse(ConfigurationManager.AppSettings["execute_every_minutes"].ToString(), out ExecuteMinutes);
            }
            catch
            {// Just in case... who knows what might have happened above or what values got put in by accident!  We do just want positives though
                if (ExecuteSeconds < 0 || ExecuteSeconds > 600)
                {// if seconds are over 10 min... doesn't need to be used
                    ExecuteSeconds = 0;
                }
                if (ExecuteMinutes < 0 || ExecuteMinutes > 60)
                {
                    ExecuteMinutes = ExecuteMinutes < 0 ? 5 : 60;
                }
            }

            Minutes = ExecuteMinutes;
            Seconds = ExecuteSeconds;
        }
    }
}

Now... what's odd is....

Click "start" for the service, then it logs:

Program Starting

*waits 20 seconds*

Entry of Manpower Notifier Callback

Next manpower hit: {the time}

Program Starting

*Service is now at "started" state*

Entry of Manpower notifier callback

Next manpower hit {the time} 

 

 

It's acting like it crashes and restarted (it's not set to auto-restart although even the routine for the new thread is encased in a try..catch and is set to record any errors)...  I actually put in logs if it executes the other thread and nothing's logged (so that code isn't even run).  

 

Basically it seems to be "wait for the timer to expire and hit" instead of "ok thread's doing it's thing, it's started"

 

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

OMg.... the issue?  I think I found it.  It was running the service as a local "something" account  (services, log on,  this account) I believe it said "local service"... switched to "local system" and it started in under 2 seconds.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×