Page 1 of 1

Delayed events

Posted: Mon Nov 25, 2013 11:20 am
by sjdebdaly
Hi all, I seem to have come up with a case of events being delayed.

I have two Analog Numerics, numerator and denominator, each attached to a tag, num and den. Each has a ValueChanged event defined, which calls a method which divides the two numbers by zero. The tags' values and the result are both pushed to a label. Both the tags have the initial value of 1.

Code: Select all

void numerator_ValueChanged(System.Object sender, Neo.ApplicationFramework.Interfaces.Events.ValueChangedEventArgs e)
{
	DivideByZero(Globals.Tags.num.Value, Globals.Tags.den.Value);
}
		
void denominator_ValueChanged(System.Object sender, Neo.ApplicationFramework.Interfaces.Events.ValueChangedEventArgs e)
{
	DivideByZero(Globals.Tags.num.Value, Globals.Tags.den.Value);
}
		
private void DivideByZero(int num, int den)
{
	try
	{
		int res = num / den;
				
		resultLabel.Text = String.Format("Result: {0} / {1} = {2}", num, den, res);
	}
	catch(DivideByZeroException)
	{
		DialogResult dlg = MessageBox.Show(	"You divided by zero!",
													"Error!",
													MessageBoxButtons.OK,
													MessageBoxIcon.Hand,
													MessageBoxDefaultButton.Button1);
				
		resultLabel.Text = String.Format("Result: {0} / {1} = NaN", num, den);
	}
}
What's going wrong:

I set the denominator to 5, the label is "1 / 1 = 1".
I set the numerator to 10, the label is "1 / 5 = 0".
I set the denominator to 0, the label is "10 / 5 = 2".
I set the denominator to 5, I get a divide by zero popup.

Each event is delayed by one - unless I set the same value in which case the event doesn't fire. This happens during simulation, running and downloading (on a T7B).

Any help is appreciated.

Re: Delayed events

Posted: Wed Dec 04, 2013 2:42 am
by sjdebdaly
I fixed this problem last week. The fault was that I was using the ValueChanged event and then using the tags attached to the object owning the event instead of using the variable supplied in the event arguments. The tags get updated after the event has fired, giving the illusion that it is one event behind.