示例#1
0
        public void CreateTimer(ServiceController.ServiceTimerParameters parameters)
        {
            if(!CanStop)
            {
                return;
            }

            //TODO: make ticks run in another thread //System.Threading.Thread.CurrentThread.ManagedThreadId

            lock(_timerList)
            {
                var timer = new Timer(parameters.Interval);
                _timerList.Add(timer);

                var errorHandlingAction = new Action
                    (() =>
                     {
                         try
                         {
                             if(parameters.Validate != null && !parameters.Validate(DateTime.Now))
                             {
                                 return;
                             }

                             parameters.Action();
                         }
                         catch(Exception e)
                         {
                             Logger.Error(e);
                             if(parameters.ErrorReporting)
                             {
                                 //TODO make error reporting class with += support, mail + webservice
                                 /*
                                 try
                                 {
 
                                     var client = new SmtpClient
                                     {
                                         Port = ?,
                                         DeliveryMethod = SmtpDeliveryMethod.Network,
                                         UseDefaultCredentials = false,
                                         Host = "?",
                                         Credentials = new NetworkCredential("?", "?")
                                     };
                                     var mail = new MailMessage("?", "?")
                                     {
                                         Subject = $"Service Exception: {AssemblyUtils.GetEntryAssembly().GetName().Name}",
                                         Body = e.ToString()
                                     };
                                     mail.CC.Add("?");
                                     
                                     client.Send(mail);
 
                                 }
                                     catch (Exception)
                                     {
                                         Logger.Error(e);
                                     }
                                 }
                          */
                             }
                         }
                     });

                timer.Elapsed += (sender, args) =>
                {
                    Logger.Trace("Timer {0} tick: {1}", sender.GetHashCode(), args.SignalTime);
                    errorHandlingAction();
                };
                errorHandlingAction();
                timer.Enabled = true;
            }
        }