Question Service not sending out messages...

cboshdave

Member
Joined
Dec 11, 2014
Messages
24
Programming Experience
1-3
I got my timer to work. But when it fails, it is supposed to send out a message. I am testing the service on a machine that should "fail" every time. I start the service, it calls the sendText routine several times. No messages are sent until I STOP the service. Then a bunch of messages come rolling in. Not sure what I need to put into the code to allow the machine to process and send the messages?? Even the start/stop messages don't seem to process until I stop the service.

C#:
        protected override void OnStart(string[] args)
        {
            timer1 = new System.Timers.Timer(60000);
            timer1.Elapsed += checkTaskManager ;
            timer1.Start();
            DateTime dtNow = DateTime.Now;
            sendText("Service is started - " + dtNow.ToString(), "ICES Service is started.  ");
        }


        protected override void OnStop()
        {
            timer1.Stop();
            DateTime dtNow = DateTime.Now;
            sendText("Service is stopped - " + dtNow.ToString(), "ICES Service is stopped.  ");
        }




        private void checkTaskManager(object sender, ElapsedEventArgs e)
        {
            string sCheckVal = "Java";
            Debugger.Break();
            if (bChecking == false && sCheckVal != "")
            {
                TaskMgrCheck tskCheck = new TaskMgrCheck();
                //read the string to check from either a text file or a command line switch. 
                int iCount = tskCheck.checkTaskMgr(sCheckVal);
                DateTime dtNow = DateTime.Now;


                //MessageBox.Show("I found " + iCount.ToString() + " instances of \"" + sCheckVal + "\"");
                if (iCount < 3 )
                {
                    //Set a bChecking value while I am working here so I don't keep sending notifications. 
                    bChecking = true;


                    string sMachineName = System.Environment.MachineName.ToString();
                    string sMessage = "Production ICES is not running on " + sMachineName +
                        " server.  There are only " + iCount.ToString() +
                        " instances of \"" + sCheckVal + "\" running.";
                    sendText("ICES Prod is Down - " + dtNow.ToString(), sMessage);


                }


                bChecking = false;
            }
        }
 
I know this is a very late reply, but I like to share how I develop services. I basically develop one application that can either be a console application or a service depending on a conditional compilation flag. This makes debugging while developing very simple and there is no need to uninstall / install the service every time that one wants to test; simply compile the console version and run it.

Create a standard service as you would normally do and add a couple of methods:
  • StartThread() simulates OnStart() when compiling as a console app
  • StopThread() simulates OnStop() when compiling as a console app
  • MethodStart() does the setup / start work that would normally go in OnStart(); it's called from OnStart() and StartThread() and provides consistency
  • MethodStop() does the cleanup work that normally would go in OnStop(); it's called from OnStop() and StopThread() and provides consistency

    public partial class Service1 : ServiceBase
    {
        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            if (!MethodStart())
            {
                // bail out
                Stop();
            }
        }

        protected override void OnStop()
        {
            if (!MethodStop())
            {
            }
        }

        /*
         * Add this for use as console application
         */

        public void StartThread()
        {
            if (!MethodStart())
            {
                // inform user
                Console.WriteLine("Could not start");
                // bail out
                return;
            }
        }

        public void StopThread()
        {
            if (!MethodStop())
            {
            }
        }

        /*
         * Methods that do the work for start and stop
         */

        /// <summary>
        /// setup
        /// </summary>
        /// <returns>false on error, else true</returns>
        public bool MethodStart()
        {
            // do the setup work here (e.g. initialize and start timer)

            return true;
        }

        /// <summary>
        /// cleanup
        /// </summary>
        /// <returns>false on error, else true</returns>
        public bool MethodStop()
        {
            // do the cleanup work here (e.g. stop the timer)

            return true;
        }
    }


Next modify the Program.cs as show below. I have defined a flag for the conditional compilation called 'IsConsoleFlag'. If the flag is set, a console application will be created, else a service will be created
        static void Main()
        {
#if ( !IsConsoleApp )
            /*
             * The standard as generated by VS to start the service
             */
            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[] 
            { 
                new Service1() 
            };
            ServiceBase.Run(ServicesToRun);
#else
            /*
             * Use as a console application
             */
            var service = new Service1();
            service.StartThread();
            Console.WriteLine("Press the Enter key to exit the program.");
            Console.ReadLine();
            service.StopThread();
#endif
        }


One way of defining the flag is to place a #define at the beginning of Program.cs
#define IsConsoleApp

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;


By removing the line or changing it to '#define notIsConsoleApp', compilation will result in a service

The better way is to define the flag in the properties of the application in the solution explorer. Open the properties of the application and select "Build" in the left column. Type 'IsConsoleApp' in the field conditional compilation symbols. This will apply the flag to all files when compiled.
To keep track of conditional compilation symbols that I use, I never remove them but rename them (as mentioned I will make it 'notIsConsoleApp').

Note:
For Console.ReadLine to work, you need to change the output type of the application from 'Windows Application' to 'Console Application'; in the properties windows, select "Application" and choose the correct output type.
 
Back
Top Bottom