none
OnTimer event doesn't be fired RRS feed

  • Question

  • OnTimer Event doesn't be fired.

    Here is the code.

        public partial class Form1 : Form
        {
            Appointment appointment;
            ReminderSchedule remindschedule;
            List<ReminderSchedule> reminderschedulelist;
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                reminderschedulelist = new List<ReminderSchedule>();
                remindschedule = new ReminderSchedule();
                appointment = new Appointment();
                appointment.ApptNo = 1;
                remindschedule.Appointment = appointment;
                remindschedule.Complete = false;
                remindschedule.ConfirmMsg = "";
                remindschedule.DailyRemindCount = 3;
                remindschedule.Timer.Interval = 1000;
                remindschedule.Timer.AutoReset = true;
                remindschedule.Timer.Elapsed += delegate { OnRemindScheduleTimerEvent(appointment.ApptNo); };
                remindschedule.Timer.Start();
                reminderschedulelist.Add(remindschedule);
    
                remindschedule = new ReminderSchedule();
                appointment = new Appointment();
                appointment.ApptNo = 2;
                remindschedule.Appointment = appointment;
                remindschedule.Complete = false;
                remindschedule.ConfirmMsg = "";
                remindschedule.DailyRemindCount = 3;
                remindschedule.Timer.Interval = 2000;
                remindschedule.Timer.AutoReset = true;
                remindschedule.Timer.Elapsed += delegate { OnRemindScheduleTimerEvent(appointment.ApptNo); };
                remindschedule.Timer.Start();
                reminderschedulelist.Add(remindschedule);
    
                remindschedule = new ReminderSchedule();
                appointment = new Appointment();
                appointment.ApptNo = 3;
                remindschedule.Appointment = appointment;
                remindschedule.Complete = false;
                remindschedule.ConfirmMsg = "";
                remindschedule.DailyRemindCount = 3;
                remindschedule.Timer.Interval = 3000;
                remindschedule.Timer.AutoReset = true;
                remindschedule.Timer.Elapsed += delegate { OnRemindScheduleTimerEvent(appointment.ApptNo); };
                remindschedule.Timer.Start();
                reminderschedulelist.Add(remindschedule);
    
    
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
    
            }
            public void OnRemindScheduleTimerEvent(int Index)
            {
                if (Index == 0)
                {
                    this.label1.Text = System.DateTime.Now.ToLongTimeString();
                }
                else if (Index == 1)
                {
                    this.label2.Text = System.DateTime.Now.ToLongTimeString();
                }
                else if (Index == 2)
                {
                    this.label3.Text = System.DateTime.Now.ToLongTimeString();
                }
            }
        }
    

    Can anybody give me some advice?

    Saturday, April 20, 2019 7:49 PM

Answers

  • Can anybody give me some advice?

    The code is not going to work, and you should abandon this approach. The object being put into a List<T> is not part of the Form class/object where the event is defined on the main thread where code is excuting.

    The object being put into the List <T> lives on its own,  a separate entity into itself,  that is independent of the Form, an object,  where the event lives and is running on the main thread.. 

    The main thread that the Form object lives on knows nothing the object that has been loaded into the List<T>,  and nothing in the object loaded in the List<T>  will be executed by  the main thread that the Form and its event lives on where code is being executed on the thread. 


    • Edited by DA924x Saturday, April 20, 2019 9:07 PM
    • Marked as answer by Jeff0803 Sunday, April 21, 2019 2:43 AM
    Saturday, April 20, 2019 9:06 PM
  • Then what can be workaround?

    I need to construct/destroy dozens of timers per day and should manage dynamically.

    I should handle timer with AppNo.

    What can be reasonable way to do this?

    Figure out a way to spawn multiple timer threads and dispose of them.

    https://docs.microsoft.com/en-us/dotnet/api/system.threading.timer?view=netframework-4.8

    https://www.c-sharpcorner.com/UploadFile/1d42da/timer-class-in-threading-C-Sharp/

    • Marked as answer by Jeff0803 Sunday, April 21, 2019 4:55 AM
    Sunday, April 21, 2019 3:37 AM
  • https://www.codeproject.com/Articles/18702/Threading-in-NET-and-WinForms

    Do you mean I use System.Threading.Timer class instead of System.Timers.Timer in the ReminderSchedule Class?

    Yes, you spawn up System.Threading.Timer for multiple timers  with each timer on its  own thread using its own delegate that has the required fuctionalty of code needed to compete the task when the time expires.

    You can't inherit from the System.Threading.Timer class  so ReminderSchedule class can't inherit from it.

    So maybe you have a static number of timer threads from t1 to t20 with a delegate for each timer. 

    Maybe, ReminderSchedule is loaded into a List<T> to reserve usgae of a timer giving the parameters to be used for a given timer that will be spawned, So if only five  ReminderSchedule are loaded into the List<T>, then 5 out of the 20 timers will be used.

    All of this initialization should done at the Form_Load event. And all the time delegates can be public methods on the form class that will be accessible and executable when the timer expires.

    • Marked as answer by Jeff0803 Sunday, April 21, 2019 4:09 PM
    Sunday, April 21, 2019 7:39 AM

All replies

  • Can anybody give me some advice?

    The code is not going to work, and you should abandon this approach. The object being put into a List<T> is not part of the Form class/object where the event is defined on the main thread where code is excuting.

    The object being put into the List <T> lives on its own,  a separate entity into itself,  that is independent of the Form, an object,  where the event lives and is running on the main thread.. 

    The main thread that the Form object lives on knows nothing the object that has been loaded into the List<T>,  and nothing in the object loaded in the List<T>  will be executed by  the main thread that the Form and its event lives on where code is being executed on the thread. 


    • Edited by DA924x Saturday, April 20, 2019 9:07 PM
    • Marked as answer by Jeff0803 Sunday, April 21, 2019 2:43 AM
    Saturday, April 20, 2019 9:06 PM
  • Then what can be workaround?

    I need to construct/destroy dozens of timers per day and should manage dynamically.

    I should handle timer with AppNo.

    What can be reasonable way to do this?

    Sunday, April 21, 2019 2:47 AM
  • Then what can be workaround?

    I need to construct/destroy dozens of timers per day and should manage dynamically.

    I should handle timer with AppNo.

    What can be reasonable way to do this?

    Figure out a way to spawn multiple timer threads and dispose of them.

    https://docs.microsoft.com/en-us/dotnet/api/system.threading.timer?view=netframework-4.8

    https://www.c-sharpcorner.com/UploadFile/1d42da/timer-class-in-threading-C-Sharp/

    • Marked as answer by Jeff0803 Sunday, April 21, 2019 4:55 AM
    Sunday, April 21, 2019 3:37 AM
  • I'm wondering if you can give me a practical idea.

    Do you mean I use System.Threading.Timer class instead of System.Timers.Timer in the ReminderSchedule Class?

    Otherwise should I totally change my structure of class?

    Somehow I need to manage timer which is mapped to a List<T>'s each item by AppNo.

    Sunday, April 21, 2019 5:05 AM
  • https://www.codeproject.com/Articles/18702/Threading-in-NET-and-WinForms

    Do you mean I use System.Threading.Timer class instead of System.Timers.Timer in the ReminderSchedule Class?

    Yes, you spawn up System.Threading.Timer for multiple timers  with each timer on its  own thread using its own delegate that has the required fuctionalty of code needed to compete the task when the time expires.

    You can't inherit from the System.Threading.Timer class  so ReminderSchedule class can't inherit from it.

    So maybe you have a static number of timer threads from t1 to t20 with a delegate for each timer. 

    Maybe, ReminderSchedule is loaded into a List<T> to reserve usgae of a timer giving the parameters to be used for a given timer that will be spawned, So if only five  ReminderSchedule are loaded into the List<T>, then 5 out of the 20 timers will be used.

    All of this initialization should done at the Form_Load event. And all the time delegates can be public methods on the form class that will be accessible and executable when the timer expires.

    • Marked as answer by Jeff0803 Sunday, April 21, 2019 4:09 PM
    Sunday, April 21, 2019 7:39 AM
  • Thanks, I'll try this scenario.
    Sunday, April 21, 2019 4:09 PM