控件中国网现已改版,您看到的是老版本网站的镜像,系统正在为您跳转到新网站首页,请稍候.......
中国最专业的商业控件资讯网产品咨询电话:023-67870900 023-67871946
产品咨询EMAIL:SALES@COMPONENTCN.COM

Windows Service

作者:佚名 出处:互联网 2011年03月16日 阅读:

Windows Service

ProjectInstaller.cs    安装文件

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;


namespace AccountService
{
    [RunInstaller(true)]
    public partial class ProjectInstaller : Installer
    {
        public ProjectInstaller()
        {
            InitializeComponent();
        }

        //This code should be inserted into your ProjectInstaller class' code


        public override void Install(IDictionary stateServer)
        {
            Microsoft.Win32.RegistryKey system,
                //HKEY_LOCAL_MACHINE\Services\CurrentControlSet
              currentControlSet,
                //...\Services
              services,
                //...\<Service Name>
              Scaning,
                //...\Parameters - this is where you can put service-specific configuration
              config;

            try
            {
                //Let the project installer do its job
                base.Install(stateServer);

                //Open the HKEY_LOCAL_MACHINE\SYSTEM key
                system = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("System");
                //Open CurrentControlSet
                currentControlSet = system.OpenSubKey("CurrentControlSet");
                //Go to the services key
                services = currentControlSet.OpenSubKey("Services");
                //Open the key for your service, and allow writing
                Scaning = services.OpenSubKey(this.serviceInstaller1.ServiceName, true);
                //Add your service's description as a REG_SZ value named "Description"
                Scaning.SetValue("Description", System.Configuration.ConfigurationManager.AppSettings["serviceDescription"].ToString());
                //(Optional) Add some custom information your service will use...
                config = Scaning.CreateSubKey("Parameters");
            }
            catch (Exception e)
            {
                Console.WriteLine("An exception was thrown during service installation:\n" + e.ToString());
            }
        }

        public override void Uninstall(IDictionary stateServer)
        {
            Microsoft.Win32.RegistryKey system,
              currentControlSet,
              services,
              service;

            try
            {
                //Drill down to the service key and open it with write permission
                system = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("System");
                currentControlSet = system.OpenSubKey("CurrentControlSet");
                services = currentControlSet.OpenSubKey("Services");
                service = services.OpenSubKey(this.serviceInstaller1.ServiceName, true);
                //Delete any keys you created during installation (or that your service created)
                service.DeleteSubKeyTree("Parameters");
                //...
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception encountered while uninstalling service:\n" + e.ToString());
            }
            finally
            {
                //Let the project installer do its job
                base.Uninstall(stateServer);
            }
        }
 
        private void serviceInstaller1_AfterInstall(object sender, InstallEventArgs e)
        {

        }

        private void serviceProcessInstaller1_AfterInstall(object sender, InstallEventArgs e)
        {

        }
    }
}


Windows Service.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using ComLib;
using Com.Auo.SSP3g;
using log4net;

 

namespace AccountService
{
    public partial class AccountService : ServiceBase
    {
        public AccountService()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            timerAccount.Interval =double.Parse(System.Configuration.ConfigurationManager.AppSettings["accountServiceTime"].ToString());
            timerAccount.Elapsed += new System.Timers.ElapsedEventHandler(RemoveAccount_Elapsed);
            timerAccount.Start();
        }

        protected override void OnStop()
        {
            timerAccount.Stop();
        }

        void RemoveAccount_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                string actionTime = System.Configuration.ConfigurationManager.AppSettings["actionTime"].ToString();
                string currentTime = DateTime.Now.ToShortTimeString();
                string judgeDate = System.Configuration.ConfigurationManager.AppSettings["judgeDate"].ToString();
                if (actionTime == currentTime)
                {
                    AccountManager accountManager = apc.GetManager<AccountManager>();
                    accountManager.DeleteInvalidAccount(judgeDate);
                }
            }
            catch (Exception ex)
            {
                Com.Auo.SSP3g.Logs.Logger.GetLogger(System.Reflection.MethodBase.GetCurrentMethod()).Error(ex.Message);
            }
        }
    }
}

 

热推产品

  • ActiveReport... 强大的.NET报表设计、浏览、打印、转换控件,可以同时用于WindowsForms谀坔攀戀Forms平台下......
  • AnyChart AnyChart使你可以创建出绚丽的交互式的Flash和HTML5的图表和仪表控件。可以用于仪表盘的创......
首页 | 新闻中心 | 产品中心 | 技术文档 | 友情连接 | 关于磐岩 | 技术支持中心 | 联系我们 | 帮助中心 Copyright-2006 ComponentCN.com all rights reserved.重庆磐岩科技有限公司(控件中国网) 版权所有 电话:023 - 67870900 传真:023 - 67870270 产品咨询:sales@componentcn.com 渝ICP备12000264号 法律顾问:元炳律师事务所 重庆市江北区塔坪36号维丰创意绿苑A座28-5 邮编:400020
在线客服
在线客服系统
在线客服
在线客服系统