Share via

Change to C#

John Ertle Jr 1 Reputation point
2022-03-13T19:07:47.273+00:00

How do I change this to C#?

Dim WithEvents timer As New System.Windows.Forms.Timer


Also what is the need for DispatcherTimer showing an error

        private void Form1_Load(System.Object sender, System.EventArgs e)
        {
            // 1000 milliseconds interval = 1 seconds
            DispatcherTimer timer = new DispatcherTimer(); 
            timer.Enabled = true;
            timer.Interval = 1000;
            timer.Start();
        }
Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.

0 comments No comments

2 answers

Sort by: Most helpful
  1. John Ertle Jr 1 Reputation point
    2022-03-13T20:43:37.89+00:00

    How do you change this to C#?

    Dim WithEvents timer As New System.Windows.Forms.Timer

    Was this answer helpful?


  2. Karen Payne MVP 35,606 Reputation points Volunteer Moderator
    2022-03-13T20:31:00.32+00:00

    Use it as shown below using C#9 or higher, adjust the TimeSpan accordingly.

    DispatcherTimer timer = new()
    {
        Interval = new TimeSpan(0, 0, 1),
    };
    
    timer.Tick += TimerOnTick;
    timer.Start();
    

    Pre .NET Core

    DispatcherTimer timer = new DispatcherTimer
    {
        Interval = new TimeSpan(0, 0, 1)
    };
    

    Event

    private void TimerOnTick(object sender, EventArgs e)
    {
    
    }
    

    Note for Windows forms you can have more control with a Timer using the code below and full source here.

    Responsible for work

    public class ActionContainer
    {
        /// <summary>
        /// Action to perform
        /// </summary>
        public Action Action { get; set; } = () => { };
    }
    

    Timer class

    using System;
    using System.Threading;
    using Timer = System.Threading.Timer;
    
    namespace YourNamespaceGoesHere
    {
        public class TimerHelper
        {
            /// <summary>
            /// How long between intervals, currently 30 minutes
            /// </summary>
            private static int _dueTime = 1000 * 60 * 30;
            private static Timer _workTimer;
            public static ActionContainer ActionContainer;
    
            /// <summary>
            /// Text to display to listener 
            /// </summary>
            /// <param name="message">text</param>
            public delegate void MessageHandler(string message);
            /// <summary>
            /// Optional event 
            /// </summary>
            public static event MessageHandler Message;
            /// <summary>
            /// Flag to determine if timer should initialize 
            /// </summary>
            public static bool ShouldRun { get; set; } = true;
    
            /// <summary>
            /// Default initializer
            /// </summary>
            private static void Initialize()
            {
                if (!ShouldRun) return;
                _workTimer = new Timer(Dispatcher);
                _workTimer.Change(_dueTime, Timeout.Infinite);
            }
    
            /// <summary>
            /// Initialize with time to delay before triggering <see cref="Worker"/>
            /// </summary>
            /// <param name="dueTime"></param>
            private static void Initialize(int dueTime)
            {
                if (!ShouldRun) return;
                _dueTime = dueTime;
                _workTimer = new Timer(Dispatcher);
                _workTimer.Change(_dueTime, Timeout.Infinite);
            }
            /// <summary>
            /// Trigger work, restart timer
            /// </summary>
            /// <param name="e"></param>
            private static void Dispatcher(object e)
            {
                Worker();
                _workTimer.Dispose();
                Initialize();
            }
    
            /// <summary>
            /// Start timer without an <see cref="Action"/>
            /// </summary>
            public static void Start()
            {
                Initialize();
                Message?.Invoke("Started");
            }
            /// <summary>
            /// Start timer with an <see cref="Action"/>
            /// </summary>
            public static void Start(Action action)
            {
                ActionContainer = new ActionContainer();
                ActionContainer.Action += action;
    
                Initialize();
    
                Message?.Invoke("Started");
    
            }
            /// <summary>
            /// Stop timer
            /// </summary>
            public static void Stop()
            {
                _workTimer.Dispose();
                Message?.Invoke("Stopped");
            }
    
            /// <summary>
            /// If <see cref="ActionContainer"/> is not null trigger action
            /// else alter listeners it's time to perform work in caller
            /// </summary>
            private static void Worker()
            {
                Message?.Invoke("Performing work");
                ActionContainer?.Action();
            }
        }
    }
    

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.