More on the NAV Timer Control Add-in

In the previous post I showed a change that I did to Freedy’s timer control.  That was not the end of my problems.  First I had the timer stop in some cases where I did not want that and in other cases I wanted to close the page in the Event Trigger.

Duilio Tacconi, a Microsoft Dynamics NAV Support Engineer found a post in Mibuso.  After reading that I removed all the thread handling and created a new timer control without.  In another page I still wanted the thread handling, that is, I wanted the timer to stop if I started a subpage or a process.  That solved my first problem.

The second problem appeared when I tried to close a page from the AddInEvent on the page.  Freddy’s code did not allow this.  The original code is

[code lang=”csharp”] /// <summary>
/// Timer tick handler – raise the Service Tier Add-In Event
/// </summary>
void timer_Tick(object sender, EventArgs e)
{
// Stop the timer while running the add-in Event
timer.Stop();
// Invoke event
this.RaiseControlAddInEvent(this.count++, "");
// Restart the timer
timer.Start();
}[/code]

and I changed it to

[code lang=”csharp”] /// <summary>
/// Timer tick handler – raise the Service Tier Add-In Event
/// </summary>
void timer_Tick(object sender, EventArgs e)
{
// Stop the timer while running the add-in Event
timer.Stop();
// Invoke event
this.RaiseControlAddInEvent(this.count++, "");
// Restart the timer
if (timer.Interval > 0)
{
timer.Start();
}
}[/code]

to make sure that the add-in did not try to start the timer if I set the interval to zero.

NAV Timer Control Add-ins

NAV Timer DotNet Addin

Freddy supplied the code to build a timer to be used in pages in the Role Tailored Client.  His blog entry about the timer is here.

Some of the comments show that developers have had the same problem that I ran into.  I change the code just a little bit.  Freddy’s code is

[code lang=”csharp”]public override string Value
{
get
{
return base.Value;
}
set
{
base.Value = value;
if (!int.TryParse(value, out interval))
{
interval = 0;
}
interval = interval * 100;
if (timer != null && timer.Interval != interval)
{
timer.Interval = interval;
count = 0;
if (interval == 0)
timer.Stop();
else
timer.Start();
}
}
}
[/code]

I moved the line “timer.Interval = interval;” down four lines.

[code lang=”csharp”]public override string Value
{
get
{
return base.Value;
}
set
{
base.Value = value;
if (!int.TryParse(value, out interval))
{
interval = 0;
}
interval = interval * 100;
if (timer != null && timer.Interval != interval)
{
count = 0;
if (interval == 0)
timer.Stop();
else
timer.Interval = interval;
timer.Start();
}
}
}
[/code]

and the problem was solved.

NAV Timer Add-in