Example #1
0
        /// <summary>
        /// 服务启动执行的操作
        /// </summary>
        /// <param name="args"></param>
        protected override void OnStart(string[] args)
        {
            if (_fTimerServices.Count == 0)
            {
                var wsiEmpty = new WindowsServiceItem
                {
                    WindowsTimer = new WindowsTimer("默认的一个Command的轮询周期,设置为5分钟。", 300000),
                    CommandType  = typeof(WindowsServiceEmptyCommand)
                };
                _fTimerServices.Add(wsiEmpty);
            }

            var wsi = new WindowsServiceItem
            {
                WindowsTimer = new WindowsTimer("默认的一个Command的轮询周期,设置为5秒钟。", 5000),
                CommandType  = typeof(WindowsServiceDefaultCommand)
            };

            _fTimerServices.Add(wsi);



            foreach (WindowsServiceItem kvp in _fTimerServices)
            {
                kvp.WindowsTimer.Timer.Elapsed -= Timer_Elapsed;
                kvp.WindowsTimer.Timer.Elapsed += Timer_Elapsed;
                kvp.WindowsTimer.Timer.Enabled  = true;
            }
        }
Example #2
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="item"></param>
 public void Remove(WindowsServiceItem item)
 {
     IsWindowsServiceEnviroments = true;
     if (List.Contains(item))
     {
         List.Remove(item);
     }
 }
Example #3
0
        private void Timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            try
            {
                #region 获取Command对象

                WindowsServiceItem wsItem = _fTimerServices.GetItemByTimer((Timer)sender);
                Type     commandType      = wsItem.CommandType;
                ICommand command          = null;
                if (!_fTimerServiceObjects.ContainsKey(commandType))
                {
                    lock (_fTimerServiceObjectsLock)
                    {
                        if (!_fTimerServiceObjects.ContainsKey(commandType))
                        {
                            var cmd = Activator.CreateInstance(commandType) as ICommand;
                            _fTimerServiceObjects.Add(commandType, cmd);
                            command = cmd;
                        }
                    }
                }
                else
                {
                    command = _fTimerServiceObjects[commandType];
                }

                #endregion

                if (!wsItem.WindowsTimer.Prepared)
                {
                    return;
                }

                if (command != null)
                {
                    lock (wsItem.WindowsTimer)
                    {
                        try
                        {
                            wsItem.WindowsTimer.Prepared = !wsItem.WindowsTimer.Prepared;
                            command.Execute();
                        }
                        catch (Exception ex)
                        {
                            //这里不应该导致整个服务终止,而只是把这个错误信号传递到外面去。
                            if (wsItem.WindowsTimer.CallBack != null)
                            {
                                var args = new WindowsServiceTimerExceptionArgs(ex, e);
                                try
                                {
                                    wsItem.WindowsTimer.CallBack(wsItem.WindowsTimer, args);
                                }
                                catch (Exception ex1)
                                {
                                    LogHelper.WriteErrorLog(ex1.ToString());
                                }
                            }
                        }
                        finally
                        {
                            wsItem.WindowsTimer.Prepared = !wsItem.WindowsTimer.Prepared;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteErrorLog(ex.ToString());
            }
        }
Example #4
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="item"></param>
 /// <returns></returns>
 public bool Contains(WindowsServiceItem item)
 {
     return(List.Contains(item));
 }
Example #5
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="item"></param>
 public void Add(WindowsServiceItem item)
 {
     IsWindowsServiceEnviroments = true;
     List.Add(item);
 }