1
\$\begingroup\$

Any idea on how to run application continuously? I tried follow suggestions from this link, but to no avail. It might be because of my coding but I don't know how to amend it. Someone suggested that I use Windows Service, but I don't want to use it at this moment because I want to learn basics first like in the above link.

Program.cs

        static int exitFlag = 0;

        private static int m_intErrCounter = 0;
        static int Main(string[] args)
        {
            int retValue = 0;

            while (exitFlag != 0)
            {
                retValue = CounterApp();
            }
            //return retValue;
            return 0;
        }

        public static int CounterApp()
        {
            string machineName = ConfigurationManager.AppSettings["MachineName"];
            string categoryName = ConfigurationManager.AppSettings["CategoryName"];
            string counterName = ConfigurationManager.AppSettings["CounterName"];
            string instanceName = ConfigurationManager.AppSettings["InstanceName"];
            PerformanceCounterCategory pcc;
            PerformanceCounter[] counters;
            m_intErrCounter = 0;

            try
            {
                // Create the appropriate PerformanceCounterCategory object.
                if (machineName.Length > 0 && instanceName.Length > 0)
                {
                    pcc = new PerformanceCounterCategory(categoryName, machineName);
                    counters = pcc.GetCounters(instanceName);
                }
                else
                {
                    pcc = new PerformanceCounterCategory(categoryName);
                    counters = pcc.GetCounters();
                }

            }
            catch (Exception)
            {
                Console.WriteLine("Couldn't found the application");
                Console.ReadKey();
                return 0;
            }

            String strBody = String.Empty;
            if (counters.Length != 0)
            {

                for (int objX = 0; objX < counters.Length; objX++)
                {
                    if ((counters[objX].CounterName == counterName) && (counters[objX].RawValue > 0))
                    {
                        strBody = "Error occured at " + counters[objX].InstanceName.ToString();
                        strBody += " : " + counters[objX].CounterName.ToString() + " thrown " + counters[objX].RawValue.ToString() + " times.";
                        m_intErrCounter++;
                        exitFlag = 1;
                    }
                }
            }
            if (m_intErrCounter > 0)
            {
                SendMail("Notification Email", strBody);
                exitFlag = 1;
            }
            return exitFlag;
        }

        private static void SendMail(String strSubj, String strBody)
        {
            MailSettingsSectionGroup mMailSettings = new MailSettingsSectionGroup();
            string mMailFrom = mMailSettings.Smtp.From;
            string mMailHost = mMailSettings.Smtp.Network.Host;
            int mMailPort = mMailSettings.Smtp.Network.Port;
            string mMailUserName = mMailSettings.Smtp.Network.UserName;
            string mMailPassword = mMailSettings.Smtp.Network.Password;

            string to = ConfigurationManager.AppSettings["To"];

            MailMessage Message = new MailMessage();
            Message.From = new MailAddress(mMailFrom);
            Message.To.Add(new MailAddress(to));

            Message.Subject = strSubj;
            Message.Body = strBody;

            SmtpClient client = new SmtpClient(mMailHost, mMailPort);

            client.EnableSsl = false;
            client.Credentials = new System.Net.NetworkCredential(mMailUserName, mMailPassword);

            client.Send(Message);
            Console.WriteLine("Success Send Message");
        }
    }
}
\$\endgroup\$
  • \$\begingroup\$ "I tried follow suggestion from this link Run Application Continuously but unsuccessfully..." what is 'unsuccessful'? Is it exiting early or not doing what you intended? \$\endgroup\$ – ashokgelal Aug 22 '11 at 2:51
  • \$\begingroup\$ not doing what I intended. When I run it just close automatically without error or success. I think the issue is looping, but i don't know how to amend it.. \$\endgroup\$ – srahifah Aug 22 '11 at 3:04
  • \$\begingroup\$ let me explain bout my application, this application(A) use to count exception error that thrown from another application(B). Once Application B thrown error, application A will send notification email. That one I manage to do it. The problem is I don't know how to run application A continuously; so every time application B thrown error, application A will send the notification. \$\endgroup\$ – srahifah Aug 22 '11 at 3:17
  • \$\begingroup\$ Welcome srahifah. Code Review is to review working code only, please refer to the faq for more details. Next time, please post similar questions on StackOverflow. \$\endgroup\$ – Steven Jeuris Aug 22 '11 at 9:11
  • \$\begingroup\$ Thanks Steven, actually I did post at StackOverflow..someone suggest me to ask here..huhu..do apologize for the inconvenience..will take note next time.. :) \$\endgroup\$ – srahifah Aug 22 '11 at 12:32
2
\$\begingroup\$

Your loop doesn't work because the initial value for exitFlag variable (which is used as condition for the loop) is zero, and it is only being set to 1 under a special set of circumstances which apparently doesn't happen when you run an application.

If you really want to loop forever and only quit application when it fails your loop, have your function return 1 unless it fails. It should look something like this:

int Main(string[] args)
{
    while (CounterApp() != 0)
    {
    }
    return 0;
}

public static int CounterApp()
{
    // do some stuff

    try
    {
        // do some stuff that might fail
    }
    catch (Exception)
    {
        return 0;
    }

    // do some other stuff

    return 1;
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.