如何在调度应用程序中的timer_elapsed中停止重复执行方法 [英] how to stop repeated execution of a method within timer_elapsed in scheduling application

查看:65
本文介绍了如何在调度应用程序中的timer_elapsed中停止重复执行方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在安排工作时遇到问题



我必须通过从App.Config文件中读取指定的预定天数来生成报告,例如



I am facing a problem while doing scheduling work

I have to generate report by reading specified scheduled days from App.Config file like

 key="ByRACFID" value="Sunday-Saturday 11:14 AM
or
  key="ByRACFID" value="Monday, Wednesday, Friday 12:30 AM
or
 key="ByRACFID" value="Saturday 12:30 AM
or
 add key="ByRACFID" value="December 31 12:30 AM





用户可以通过上述任何方式更改预定格式。



所以在计时器已经过去的事件中我把





User can change the scheduled format in any way as mentioned above.

so in timer elapsed event i put

  public static void _timer_Elapsed(object sender, ElapsedEventArgs e)
        {

            _l.Add(DateTime.Now);
            
          

           
            SchedulingRACF("ByRACFID", ConfigurationManager.AppSettings["ByRACFID"].ToString());
           
           
        }


//SchedulingRACF in depth:

   public static void SchedulingRACF(string ReportType, string StrScheduleInfo)
      {
           
          int[] ArrDaysOfWeek = new int[] { 0, 1, 2, 3, 4, 5, 6 };// To Hold the values of all days of week
          List<int> ArrSchDays = new List<int>();// To Hold the Scheduled Days for running cycle
          try
          {

             
              string StrScheduleTime;
              string StrScheduleDay;



              //'To store cycle start day and End Day
              String StrFromDay, StrToDay;

              //'To store Current Day Number
              int StrCurrentWeekDayNum;

              if (StrScheduleInfo.Length > 0)
              {

                  StrScheduleTime = StrScheduleInfo.Substring((StrScheduleInfo.IndexOf(":") - 2)); //To Split the Time part e.g. 12:30 AM



                  StrScheduleDay = StrScheduleInfo.Substring(0, (StrScheduleInfo.IndexOf(":") - 2)); // To Split the Day part  e.g. Monday-Saturday

                  if ((StrScheduleDay.Length) > 0)
                  {
                      if (StrScheduleDay.IndexOf("-") > 0) //for weekdays days e.g.: Monda-saturday,saturday-wednesday
                      {


                          StrFromDay = FetchDayNumber(StrScheduleDay.Substring(0, (StrScheduleDay.IndexOf("-")))).ToString(); //To store integer value of a particular day


                          StrToDay = FetchDayNumber(StrScheduleDay.Substring((StrScheduleDay.IndexOf("-") + 1))).ToString(); //To store integer value of a particular day
                          StrCurrentWeekDayNum = (int)DateTime.Now.DayOfWeek;

                          if (StrFromDay != StrToDay)
                          {

                              if (int.Parse(StrFromDay) <= int.Parse(StrToDay))
                              {
                                  for (int i = int.Parse(StrFromDay); i <= int.Parse(StrToDay); i++)
                                  {

                                      ArrSchDays.Add(ArrDaysOfWeek[i]);
                                  }
                              }
                              else if (int.Parse(StrFromDay) > int.Parse(StrToDay))
                              {
                                  for (int i = int.Parse(StrFromDay); i <= ArrDaysOfWeek.Length - 1; i++)
                                  {
                                      ArrSchDays.Add(ArrDaysOfWeek[i]);
                                  }
                                  for (int j = 0; j <= int.Parse(StrToDay); j++)
                                  {
                                      ArrSchDays.Add(ArrDaysOfWeek[j]);
                                  }
                              }
                              if (int.Parse(StrFromDay) > int.Parse(StrToDay) && int.Parse(StrFromDay) != 6 && int.Parse(StrToDay) != 0)
                              {
                                  for (int j = 0; j <= int.Parse(StrToDay); j++)
                                  {
                                      if (!ArrSchDays.Contains(ArrDaysOfWeek[j]))
                                      {
                                          ArrSchDays.Add(ArrDaysOfWeek[j]);
                                      }
                                  }
                              }

                              if (int.Parse(StrToDay) == 0) //If Sunday is cycle end day
                              {
                                  ArrSchDays.Add(ArrDaysOfWeek[int.Parse(StrToDay)]);
                              }

                              foreach (int Currentday in ArrSchDays)
                              {
                                  if (StrCurrentWeekDayNum == Currentday)
                                  {
                                      if (StrScheduleTime == DateTime.Now.ToString("hh:mm tt"))
                                      {
                                          
                                              GenerateReport gnr = new GenerateReport();

                                              gnr.GetRacfReport();
                                          
                                         

                                      }

                                  }
                              }

                          }
                          else
                          {
                              MessageBox.Show("Start Date and End Date canoot be same");
                          }

                      }



                      //for specific days e.g: Monday, Wednesday, Friday 12:30 AM
                      else if (StrScheduleDay.IndexOf(",") > 0)
                      {


                          StrCurrentWeekDayNum = (int)DateTime.Now.DayOfWeek;


                          if (StrScheduleDay.Contains(DateTime.Now.DayOfWeek.ToString()))
                          {
                              if (StrScheduleTime == DateTime.Now.ToString("hh:mm tt"))
                              {


                                      GenerateReport gnr = new GenerateReport();

                                      gnr.GetRacfReport();
                                  
                              }

                             
                          }
                      }

                         //for a particular day in a  Month e.g. December 31 12:30 AM

                      else if (StrScheduleDay.Contains(DateTime.Now.ToString("MMMM")) && StrScheduleDay.Contains(DateTime.Now.Day.ToString()))
                      {
                          if (StrScheduleTime == DateTime.Now.ToString("hh:mm tt"))
                          {
                              
                                  GenerateReport gnr = new GenerateReport();

                                  gnr.GetRacfReport();
                             

                          }

                      }
                      //for as specific dayno e.g. Wednesday 11:14 AM
                      else
                      {
                          StrCurrentWeekDayNum = (int)DateTime.Now.DayOfWeek;


                          if (StrScheduleDay.Contains(DateTime.Now.DayOfWeek.ToString()))
                          {
                              if (StrScheduleTime == DateTime.Now.ToString("hh:mm tt"))
                              {

                                  
                                      GenerateReport gnr = new GenerateReport();

                                      gnr.GetRacfReport();
                                 

                              }

                              
                          }
                      }
                                         
                  }

              }
              else
              {
                  MessageBox.Show("Please enter proper Values in config file");
              }
          }
          catch (Exception Ex)
          {
               MessageBox.Show(Ex.Message);
          }

          finally
          {

              ArrSchDays.Clear();
          }
      }





我的问题是方法gnr.GetRacfReport()被调用多次。

我希望它只能拨打一次电话。



请建议解决方案。



My problem is the method gnr.GetRacfReport() is getting called more than one time.
I want it to call only single time .

Please suggest the solution.

推荐答案

它看起来不是一个好问题。你自己重复执行一些代码,然后询问如何只调用一次。如果组织了一些代码被重复执行,你为什么不关心其中一种避免重复的方法呢?你是自己代码的拥有者吗?



无论如何,为了以防万一,请参阅我的CodeProject文章:希望你在这里......只有一次 [ ^ ]。



< dd> -SA
It does not look like a good question. You execute some code repetitively by yourself, and then ask how to call something only once. If you organize that some code was executed repetitively, why cannot you take care about one of the methods to avoid repetition? Are you an owner of your own code or not?

Anyway, just in case, please see my CodeProject article: Wish You Were Here… Only Once[^].

—SA


System.Threading.Timer和System.Timers.Timer都通过线程池调用它们的tick方法。这意味着如果您的处理程序执行时间比执行代码之间的时间长,则可能会重新进入。这也意味着你不知道调用处理程序的哪个线程。



最简单的做法是将整个处理程序粘贴在一个锁定语句中。不确定我喜欢这个想法,因为你可能会让大量的线程排在一起。



第二个最简单的事情就是放某种类型的'AlreadyProcessing'类型标志,当已经设置时,只是退出处理程序。您应该使用某种锁或WaitHandle来同步它。
Both the System.Threading.Timer and the System.Timers.Timer call their tick methods via the threadpool. This means that if your handler takes longer to execute than the time between ticks your are likely to get re-entrancy. It also means you have no idea which thread the handler gets called on.

The simplest thing to do is to stick the whole handler in a lock statement. Not sure I like the idea of that though, as you could potentially get a large number of threads queuing up behind each other.

The second simplest thing to do is put some sort of 'AlreadyProcessing' type flag in and when already set just drop out of the handler. You should synchronise this either by using a lock or a WaitHandle of some sort.


这篇关于如何在调度应用程序中的timer_elapsed中停止重复执行方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆