如何连续运行应用程序? [英] How to run application continuously?

查看:98
本文介绍了如何连续运行应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

知道如何在下面的应用程序中连续运行吗?
我尝试使用回调方法并设法运行该应用程序;但问题是它保持每秒发送电子邮件.
我不知道如何使该应用程序连续运行并在抛出异常时发送电子邮件.如果您不理解我的查询,请告诉我.
感谢您对此的反馈.
谢谢!!!

Hi Guys,

Any idea how to run this below application continuously?
I tried used callback method and manage to run the application; but the problem is it keep send the email every second.
I dont''t know how to make this application run continuously and send the email if got exception thrown. Please do let me know if you guys didn''t understand my query.
Appreciate your feedback about this.
Thanks!!!

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.Net.Mail;

namespace CounterAppl
{
    class Program
    {
        private static int m_intErrCounter = 0;
        static void Main(string[] args)
        {   
            //run application
            TimerCallback callback = Program.CounterApp;
            Timer stateTimer = new Timer(callback, null, 0, 1000);            
            //loop here forever         
            for (; ; ) { }
        }

        public static void CounterApp(Object stateInfo)
        {
            AutoResetEvent autoEvent = (AutoResetEvent)stateInfo;
            string machineName = "ComputerName";
            string categoryName = ".NET CLR Exceptions";
            string counterName = "# of Exceps Thrown";
            string instanceName = "ApplicationName";
            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;
            }

            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++;

                    }
                }                
            }
            if (m_intErrCounter > 0)
            {
                SendMail("Notification Email", strBody);
            }
            Console.ReadKey();
            return;
        }

        private static void SendMail(String strSubj, String strBody)
        {
            MailMessage Message = new MailMessage();
            Message.From = new MailAddress("mail@company.com");
            Message.To.Add(new MailAddress("mail@company.com"));
            Message.Subject = strSubj;
            Message.Body = strBody;

            SmtpClient client = new SmtpClient("smtp.company.com", 25);

            client.EnableSsl = false;
            client.Credentials = new System.Net.NetworkCredential("email@company.com", "Pass1234");

            client.Send(Message);
            Console.WriteLine("Success Send Message");
        }
    }    
}

推荐答案

将其转换为Windows服务.
Turn it into a windows service.


正如约翰·西蒙斯(John Simmons)所说的那样,这是最好的选择.如果您不想创建Windows服务,则可以使用以下代码,但不建议使用.

方法末尾的
As John Simmons said that is the best option. If you dont want to create a windows service then you can use the below code, but not recommended.

while (true)
{
       TimerCallback callback = Program.CounterApp;
       Timer stateTimer = new Timer(callback, null, 0, 1000);
}


使用递归再次调用它.


这篇关于如何连续运行应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆