Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Timer says it's enabled, but never executes

Tags:

c#

winforms

timer

I have a sub which starts one of two timers (depending on 'zone' condition). This sub called 'CheckAndActivateRelays' is itself called by a Serial Port _DataReceived event. I am inserting break points to help me troubleshoot and am seeing that the tmrSoundSirensAfterDelay.Start() line is being executed successfully with the status of the timer even changing to enabled. However the associated Tick event never executes any of the code contained within it.

If I do the same thing by calling the sub from within button24's click event, it works perfectly. Everything is on the same Form with no threaded processes.

Anyone? Thanks

private void checkAndActivateRelays(int zoneNumber)
   {

       if (globalFullAlarmSet || globalNightAlarmSet || globalDoorsAlarmSet)
           {
               if (zoneNumber == 1) //Entry zone
               {
                   //kick off a timer after delay specified in Settings1 file,
                   if (Settings1.Default.alarmSirenDurationInMinutes != 0)
                   {
                       //activates the relays if global alarm flags are still set to true 
                       //(i.e. user has not entered code in time)
                       globalAlarmEntryDurationTicks = 0;                           
                       tmrSoundSirensAfterDelay.Start();

                   }
               }
               else //If any other zone is activated during alarm set condition
               {
                   if (Settings1.Default.alarmSirenDurationInMinutes != 0)
                   {
                       //Output to relays 1 & 2
                       spIOCard.Write("~out10=1~");
                       spIOCard.Write("~out11=1~");

                       //then close after duration from Settings1 file
                       globalAlarmSirenDurationTicks = 0;
                       tmrSoundSirens.Start();
                   }
               }

           }

   }

   private void tmrSoundSirensAfterDelay_Tick(object sender, EventArgs e)
   {
       globalAlarmEntryDurationTicks = globalAlarmEntryDurationTicks + 1;

       if (globalAlarmEntryDurationTicks == Settings1.Default.alarmEntryDelayInSeconds) //Value from Settings1 file
       {
           spIOCard.Write("~out10=1~");
           spIOCard.Write("~out11=1~");
           globalAlarmEntryDurationTicks = 0;
           tmrSoundSirensAfterDelay.Stop();
           tmrSoundSirens.Start();
       }
   }

   private void tmrSoundSirens_Tick(object sender, EventArgs e)
   {           
       globalAlarmSirenDurationTicks = globalAlarmSirenDurationTicks + 1;

       if (globalAlarmSirenDurationTicks == (Settings1.Default.alarmSirenDurationInMinutes * 5))  //*60 Value from Settings1 file
       {
           spIOCard.Write("~out10=0~");
           spIOCard.Write("~out11=0~");               
           globalAlarmSirenDurationTicks = 0;
           tmrSoundSirens.Stop();
       }
   }



private void button24_Click(object sender, EventArgs e)
   {
       globalFullAlarmSet = true;
       checkAndActivateRelays(1);
   }

Serial Port Data Received Code:

 private void spIO_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
    {
       RxString = spIOCard.ReadExisting();

       if (RxString == "~in00=1~")
       {
           checkAndActivateRelays(1);
           button10.BackColor = System.Drawing.Color.Red;               
       }

       if (RxString == "~in00=0~")
       {
           button10.BackColor = System.Drawing.Color.LightGray;
       }

       if (RxString == "~in01=1~")
       {
           checkAndActivateRelays(2);
           button11.BackColor = System.Drawing.Color.Red;
       }

       if (RxString == "~in01=0~")
       {
           button11.BackColor = System.Drawing.Color.LightGray;
       }

       if (RxString == "~in02=1~")
       {
           button12.BackColor = System.Drawing.Color.Red;
       }

       if (RxString == "~in02=0~")
       {
           button12.BackColor = System.Drawing.Color.LightGray;
       }

}

like image 482
Nathan Andrews Avatar asked Jan 24 '26 19:01

Nathan Andrews


1 Answers

Something to think about since you are using the DataReceivedEvent. According to MSDN it is raised on a secondary thread. This is probably causing your issue.

The DataReceived event is raised on a secondary thread when data is received from the SerialPort object. Because this event is raised on a secondary thread, and not the main thread, attempting to modify some elements in the main thread, such as UI elements, could raise a threading exception. If it is necessary to modify elements in the main Form or Control, post change requests back using Invoke, which will do the work on the proper thread.

like image 174
Mark Hall Avatar answered Jan 27 '26 08:01

Mark Hall



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!