Running an application continuously 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 &gt; 0 &amp;&amp; instanceName.Length &gt; 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 &lt; counters.Length; objX++)
                {
                    if ((counters[objX].CounterName == counterName) &amp;&amp; (counters[objX].RawValue &gt; 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 &gt; 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");
        }
    }
}
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;
}