Ejemplo n.º 1
0
 /// <summary>
 /// 构造函数
 /// </summary>
 /// <param name="timerServices"></param>
 public WindowsService(WindowsServiceItemCollection timerServices)
 {
     if (timerServices != null)
     {
         foreach (WindowsServiceItem item in timerServices)
         {
             _fTimerServices.Add(item);
         }
     }
 }
        /// <summary>
        /// 执行Windows服务
        /// </summary>
        public static void RunServices(WindowsServiceItemCollection timerServices, WindowsServiceTimerExceptionCallBack callBack)
        {
            WindowsServiceItemCollection.IsWindowsServiceEnviroments = true;

            try
            {
                if (timerServices == null)
                {
                    throw new ArgumentNullException("timerServices");
                }

                if (callBack != null)
                {
                    foreach (WindowsServiceItem kvp in timerServices)
                    {
                        kvp.WindowsTimer.CallBack += callBack;
                    }
                }

                var servicesToRun = new ServiceBase[]
                {
                    new WindowsService(timerServices)
                };

                foreach (ServiceBase service in servicesToRun)
                {
                    service.AutoLog = true;
                }

                AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;

                ServiceBase.Run(servicesToRun);
            }
            catch (Exception ex)
            {
                LogHelper.WriteErrorLog(ex.ToString());
                throw;
            }
        }