Questions tagged [system.timers.timer]

System.Timers.Timer is part of the .NET framework and is a server-based timer, which allows you to specify a recurring interval at which the Elapsed event is raised in your application. You can then handle this event to provide regular processing.

The Timer component is a server-based timer, which allows you to specify a recurring interval at which the Elapsed event is raised in your application. You can then handle this event to provide regular processing. For example, suppose you have a critical server that must be kept running 24 hours a day, 7 days a week. You could create a service that uses a Timer to periodically check the server and ensure that the system is up and running. If the system is not responding, the service could attempt to restart the server or notify an administrator.

The server-based Timer is designed for use with worker threads in a multithreaded environment. Server timers can move among threads to handle the raised Elapsed event, resulting in more accuracy than Windows timers in raising the event on time.

The Timer component raises the Elapsed event, based on the value of the Interval property. You can handle this event to perform the processing you need. For example, suppose that you have an online sales application that continuously posts sales orders to a database. The service that compiles the instructions for shipping operates on a batch of orders rather than processing each order individually. You could use a Timer to start the batch processing every 30 minutes.

113 questions
23
votes
1 answer

C# Blazor: Countdown Timer

I'm new to C# and trying to create a simple countdown timer using System.Timer.Timers. It didn't work as expected and I searched the internet for a solution but it didn't really fix my problem. What I want is when the user clicks the start button,…
TheNoobProgrammer
  • 1,013
  • 4
  • 10
  • 21
22
votes
3 answers

System.Timers.Timer How to get the time remaining until Elapse

Using C#, how may I get the time remaining (before the elapse event will occur) from a System.Timers.Timer object? In other words, let say I set the timer interval to 6 hours, but 3 hours later, I want to know how much time is remaining. How would I…
Lonnie Best
  • 9,936
  • 10
  • 57
  • 97
18
votes
4 answers

Can the Elapsed callback of a System.Timers.Timer be async?

Is it possible (or even reasonable) to make the callback of a System.Timers.Timer an async method? Something like: var timer = new System.Timers.Timer { Interval = TimeSpan.FromSeconds(30).TotalMilliseconds, AutoReset = true }; timer.Elapsed…
David Rubin
  • 1,610
  • 1
  • 17
  • 28
9
votes
2 answers

UWP equivalent of Timer.Elapsed event

I need to fire an event automatically every few minutes. I know I can do this using Timers.Elapsed event in Windows Forms applications as below. using System.Timers; namespace TimersDemo { public class Foo { System.Timers.Timer…
Devraj Gadhavi
  • 3,541
  • 3
  • 38
  • 67
8
votes
2 answers

How should the clean-up of Timers declared inside the scope of a function be managed?

In the following code, a Timer is declared inside a function, where it also subscribes to the Elapsed event: void StartTimer() { System.Timers.Timer timer = new System.Timers.Timer(1000); timer.Elapsed += new…
Alfie
  • 2,341
  • 2
  • 28
  • 45
8
votes
2 answers

SignalR - Server side method to detect if a client disconnects from a hub?

I'm wanting to stop a System.Timers.Timer that is running in a SignalR hub after a client closes a window/tab containing the active connection. I have tried sending a bool value to the server by calling server code to notify the server the client…
Cameron Tinker
  • 9,634
  • 10
  • 46
  • 85
4
votes
4 answers

c# Task cancellation when using System.Timers

I'm unsure how best to cancel a task that is running a system timer. In the code below, every 60 mins the timer will elapse and then run another method (CheckFileOverflow) that is used to check the file size of a system log txt. file Cancellation…
OJB1
  • 2,245
  • 5
  • 31
  • 63
4
votes
5 answers

System.Timers.Timer not working edit [in asp.net web forms]

I am trying out the Timer class with this code:- protected void Page_Load(object sender, EventArgs e) { System.Timers.Timer tm = new System.Timers.Timer(); tm.Elapsed += new System.Timers.ElapsedEventHandler(tm_Elapsed); tm.Interval =…
Soham Banerjee
  • 465
  • 1
  • 8
  • 18
4
votes
3 answers

Timer randomly doesn't fire

I have code that looks like this mTestModeMetadataTimer = new System.Threading.Timer(SomeTimerCallback, null, 1000, Timeout.Infinite); Stopwatch tmStopwatch = new Stopwatch(); private void SomeTimerCallback(object state) { // doing minimal…
Afcrowe
  • 129
  • 4
3
votes
1 answer

No overload for 'onTimer' matches delegate 'ElapsedEventHandler'

Usually when people get a "No overload for 'onTimer' matches delegate 'ElapsedEventHandler'" error, it's due to not including the ElapsedEventArgs parameter in the void that runs every time the timer has elapsed. Also, the error seems to disappear…
Golden_Eagle
  • 124
  • 1
  • 9
3
votes
3 answers

Use timer with fileSystemWatcher in C#

The scenario is that I have a root folder to monitor any new folder (that contains files) and set a timer to zip each of them individually. However, I can't tell if the file in the folder is the last file before calling the zip function, and…
Stu_Dent
  • 370
  • 1
  • 4
  • 19
3
votes
2 answers

C#, WinForms, waiting for timers

Trying to understand Timers and virtual clicks in C# Winforms. I want to have the program have an entered time value by the user (textbox1), then wait that amount of time and click the mouse, then increase the number counter (textbox2). In the code…
ShoTo
  • 153
  • 1
  • 10
3
votes
1 answer

Windows Service Exception Handling and Timers

I've an issue with System.Timers.Timer. Precisely it doesn't throw any Exceptions. Here is my code: private Timer MyTimer { get; set; } public MyService() { InitializeComponent(); AppDomain.CurrentDomain.UnhandledException +=…
3
votes
1 answer

How to prevent a C# System.Timers timer event from blocking subsequent events?

I have a C# application that uses a System.Timers timer that repeatedly calls a function. The problem is, depending on the workload, if the processing from a function call reaches a certain CPU usage percentage (98-100%), any subsequent events are…
reformed
  • 4,505
  • 11
  • 62
  • 88
2
votes
3 answers

Blazor Server Side Timer is running amok

I have this ServerSide Blazor running and I have: listTimer = new System.Timers.Timer(); listTimer.Interval = 1000; listTimer.Elapsed += async (s, e) => { await Utils.LogToConsole(jsRuntime,…
1
2 3 4 5 6 7 8