Click or drag to resize

EventTimer

Using any .NET timer classes other than the EventTimer could cause the unexpected behavior in Axiom clients, or in some cases crash the Axiom service. Also note that any long running callbacks may affect Axiom clients or servers negatively. This timer executes code in a thread safe manner that also implements a try/catch wrapper around the timer callback execution.

The following code updates a label with the current time when the timer callback is executed.

using AxiomCore2.Client;
using AxiomCore2.ControlProperties;
using AxiomCore2.Controls;
using AxiomCore2.Data;
using AxiomCore2.Events;
using AxiomCore2.Legacy;
using AxiomCore2.Log;
using AxiomCore2.Managers;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Collections;
using System.Drawing;

namespace AxiomScript
{
    public partial class ScreenScript : IDisposable
    {
        private EventTimer _timer = null;
        private int _initialTime = Convert.ToInt32(TimeSpan.FromSeconds(1).TotalMilliseconds);
        private int _interval = Convert.ToInt32(TimeSpan.FromSeconds(5).TotalMilliseconds);

        // NOTE: click help icon in dialog header for API documentation and examples.
        // NOTE: use EventTimer class when timer is necessary. See API documentation.

        // axiom references
        public ControlApplication Application { get; } = ControlApplication.Instance;
        public ControlFactoryManager ControlFactory { get; } = ControlFactoryManager.Instance;
        public IDataProvider DataProvider { get; } = DataProviderManager.CreateInstance();
        public ILog Log { get; } = ClientLog.UserScript;
        public NavigationManager Navigation { get; } = NavigationManager.Instance;
        public ControlScreen Screen => _screen;

        public void OnScreenVisible()
        {
            if (_timer == null)
            {
                // initialize timer
                _timer = new EventTimer(
                    callback: TimerCallback,
                    dueTime: _initialTime,
                    period: _interval);
            }
            else
            {
                // reset timer
                _timer.Change(
                    dueTime: _initialTime,
                    period: _interval);
            }
        }

        public void OnScreenInvisible()
        {
            if (_timer != null)
            {
                // pause timer
                _timer.Change(
                    dueTime: EventTimer.Infinite,
                    period: EventTimer.Infinite);
            }
        }

        public void Dispose()
        {
        }

        private void TimerCallback(EventTimer timer, object userData)
        {
            // update Label with current time
            Label1.Text = DateTime.Now.ToString();
        }
    }
}