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.
How do you change this to C#?
Dim WithEvents timer As New System.Windows.Forms.Timer
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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();
}
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.
How do you change this to C#?
Dim WithEvents timer As New System.Windows.Forms.Timer
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();
}
}
}