Click here to Skip to main content
15,905,867 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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

C#
  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();
          }
      }


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.
Posted
Updated 25-Feb-14 6:45am
v4
Comments
Sergey Alexandrovich Kryukov 25-Feb-14 9:14am    
Well, call it only one time... :-)
—SA
sarthakm 25-Feb-14 9:16am    
I am as the Time format is HH:MM it is getting called several time untill that particular minutes is over. as timer elapsed is calling a static method.

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
 
Share this answer
 
Comments
sarthakm 25-Feb-14 9:24am    
Sergey Its a scheduler the method will get called on daily basis so after next day again this method needed to be executed in a particular time. suppose it is scheduled on 11:14 AM for weekdays.
so daily on 11:14 AM the method will be called. But here the method gets called repeatedly untill time becomes 11:15 AM
sarthakm 25-Feb-14 9:25am    
Here is the timer events calling the method:
public static void Start()
{

_l = new List<datetime>(); // Allocate the list
_timer = new System.Timers.Timer(1000); // Set up the timer for 3 seconds
//
// Type "_timer.Elapsed += " and press tab twice.
//
_timer.Elapsed += new ElapsedEventHandler(_timer_Elapsed);



_timer.Enabled = true; // Enable it




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

_l.Add(DateTime.Now);




SchedulingMIO("MIOSummary", ConfigurationManager.AppSettings["MIOSummary"].ToString());
SchedulingDept("ByDept", ConfigurationManager.AppSettings["ByDept"].ToString());
SchedulingRACF("ByRACFID", ConfigurationManager.AppSettings["ByRACFID"].ToString());
// SetupTimerNextInterval((System.Timers.Timer)sender);


}
Maciej Los 25-Feb-14 12:49pm    
+5!
By The Way: very interesting article!
Sergey Alexandrovich Kryukov 25-Feb-14 13:27pm    
Thank you, Maciej.
—SA
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900