Manage services on the Web page

Source: Internet
Author: User

A requirement of a Company project is to send emails to some users at regular intervals. It is quite simple to think about it. You only need to enable a timer in Global. ascx, and then execute the task regularly. However, the running result is not what I think. The code I wrote is as follows:

Private System. timers. timer m_timer; void Application_Start (object sender, EventArgs e) {m_timer = new System. timers. timer (60000); // set the interval m_timer.Enabled = true; m_timer.Elapsed + = new System. timers. elapsedEventHandler (run); // enable the clock. M_timer.Start ();}

 

The Timer can be started only when the webpage is accessed for the first time. That is to say, when the website is restarted, You need to manually access any of my pages to activate the Timer, that's all about it. I am the one who restarts it. Just pay attention to it. However, after several days of testing, we found that the timer has not been executed until a certain period of time, and the timer stops working. Baidu later found a saying that after the application pool is recycled, we still need to activate the Timer again, and the default IIS reclaim time is 29 hours. I can't help it. After all, this station will always be accessed. Repeat the activation process. If the program can be used, you can repeat it.

After a few days of testing, I still found that Timer would stop working. Baidu once said that such a Timer object was not referenced in the object pool. When the garbage collection mechanism is started, the Timer will also be recycled, whether or not you are running. I am experiencing a crash here. A simple timer has encountered so many problems. Even if we don't need a timer, we can switch to a service instead. This should always be done, Baidu, I found a management service class and wrote a test page to test it. The result is good.

Test Page code:

Protected void Page_Load (object sender, EventArgs e) {string serverName = "Monitoring"; string filePath = "C: \ Documents and Settings \ Administrator \ My Documents ents \ Visual Studio 2010 \ Projects \ ServiceHelloWorld \ bin \ Debug \ ServiceHelloWorld.exe "; bool serviceIsExist = ServerHelper. DAL. serviceHelper. serviceExist (serverName); if (serviceIsExist) {Common. common. logManager. getInstance (). debug ("Uninstall"); ServerHelper. DAL. serviceHelper. unInstallService (filePath, serverName); Response. write ("Uninstall");} else {// string serviceName = ""; Common. common. logManager. getInstance (). debug ("Install"); ServerHelper. DAL. serviceHelper. installService (filePath, serverName); Response. write (serverName); Response. write ("Install ");}

Code for installing the service:

/// <Summary> /// install the service /// </summary> /// <param name = "stateSaver"> </param> /// <param name = "filepath"> </param> // <param name = "serviceName"> </param> public static void InstallService (string filepath, string serviceName) {System. serviceProcess. serviceController service = new System. serviceProcess. serviceController (serviceName); if (! ServiceExist (serviceName) {AssemblyInstaller myAssemblyInstaller = new AssemblyInstaller (); myAssemblyInstaller. useNewContext = true; myAssemblyInstaller. path = filepath; IDictionary stateSaver = new Hashtable (); stateSaver. clear (); myAssemblyInstaller. install (stateSaver); myAssemblyInstaller. commit (stateSaver); myAssemblyInstaller. dispose (); service. start ();} else {if (service. status! = System. ServiceProcess. ServiceControllerStatus. Running & service. Status! = System. ServiceProcess. ServiceControllerStatus. StartPending) {service. Start ();}}}

Code for uninstalling the service:

/// <Summary> /// uninstall the service /// </summary> /// <param name = "filepath"> </param> /// <param name = "serviceName"> </param> public static void UnInstallService (string filepath, string serviceName) {try {if (ServiceExist (serviceName) {AssemblyInstaller myAssemblyInstaller = new AssemblyInstaller (); myAssemblyInstaller. useNewContext = true; myAssemblyInstaller. path = filepath; myAssemblyInstaller. uninstall (null); myAssemblyInstaller. dispose () ;}} catch (Exception ex) {throw new Exception ("unInstallServiceError/n" + ex. message );}}

 

With such a method, I will no longer use Timer to process scheduled transactions. Moreover, it is said that big cows do not use Timer to do transactions and use services.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.